0

I have three fragments within an activity. These fragments are acting as tabs. I have various EditText in each fragment. I want to save all the data in SQLite when I press submit button in 3rd fragment. I am creating a table in first fragment and updating them in second and third fragment.

But as soon as I move to the second Fragment and save the data null reference error comes on other two pages.Right now i am saving all the data on click of button in that fragment. This is my code.

FIRSTFRAGMENT.JAVA

 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
   saveMe = (Button) view.findViewById(R.id.button1);
    compName = (EditText)  view.findViewById(R.id.edtCompName);
    parentComp =(EditText) view.findViewById(R.id.edtParComp);
    specilities = (EditText) view. findViewById(R.id.edtSpeciali);
    globHuntClient = (EditText) view.findViewById(R.id.edtGloHunt);
    comptition = (EditText) view. findViewById(R.id.edtCompe);

    noPoach = (EditText) view. findViewById(R.id.edtNoPoach);
    blackList = (EditText) view. findViewById(R.id.edtBlacklist);
    boardNo = (EditText) view. findViewById(R.id.edtBoardNo);
    industry = (EditText) view. findViewById(R.id.edtIndustry);

    indusClassi = (EditText) view. findViewById(R.id.edtIndusClassi);
    induSubClassi = (EditText) view. findViewById(R.id.edtIndSub);
    type = (EditText) view. findViewById(R.id.edtType);
    website = (EditText) view. findViewById(R.id.edtWeb);
    product = (EditText) view. findViewById(R.id.edtProd);
    service = (EditText) view. findViewById(R.id.edtService);
    toolsAndTech = (EditText) view. findViewById(R.id.edtToolsTech);
    interRecTeam = (EditText) view. findViewById(R.id.edtIntRecTeam);
    revePotinPeriod = (EditText) view. findViewById(R.id.edtRevPot);
    accoutCate = (EditText) view. findViewById(R.id.edtAccCat);
    process = (EditText) view. findViewById(R.id.edtProcess);

  mydb=new DBHelper(view.getContext(),null,null,1);

    saveMe.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DetailClass details = new DetailClass("");
            details.setCompName(compName.getText().toString());
            details.setParentComp(parentComp.getText().toString());
            details.setSpecilities(specilities.getText().toString());
            details.setGlobHunt(globHuntClient.getText().toString());
            details.setComptition(comptition.getText().toString());
            details.setnoPoach(noPoach.getText().toString());
            details.setblackList(blackList.getText().toString());
            details.setboardNo(boardNo.getText().toString());
            details.setindustry(industry.getText().toString());

            details.setindustryClassi(indusClassi.getText().toString());
            details.setindustrySubClassi(induSubClassi.getText().toString());
            details.settype(type.getText().toString());
            details.setwebsite(website.getText().toString());
            details.setproduct(product.getText().toString());
            details.setService(service.getText().toString());
            details.setToolsAndTech(toolsAndTech.getText().toString());
            details.setInterRecTeam(interRecTeam.getText().toString());
            details.setRevePotiPeriod(revePotinPeriod.getText().toString());
            details.setAccountCategory(accoutCate.getText().toString());
            details.setProcess(process.getText().toString());




            boolean getData = mydb.addLeadData(details);
            if (getData == true) {

               Toast.makeText(getActivity.this,"row inserted",Toast.Length_Long).show();
                getActivity().finish();
            }

        }
    });

SECONDFRAGMENT.JAVA

  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    firName = (EditText) view.findViewById(R.id.edtFirstName);
    middName = (EditText)  view.findViewById(R.id.edtMiddleName);
    lastName =(EditText) view.findViewById(R.id.edtLastName);
    title = (EditText) view. findViewById(R.id.edtTitle);
    subTitle = (EditText) view. findViewById(R.id.edtSubTitle);
    department = (EditText) view.findViewById(R.id.edtDepartment);
    designation = (EditText) view. findViewById(R.id.edtDesignation);

    level = (EditText) view. findViewById(R.id.edtLevel);
    reporTo = (EditText) view. findViewById(R.id.edtReporting);
    jobGrade = (EditText) view. findViewById(R.id.edtJobGrade);
    officePhone = (EditText) view. findViewById(R.id.edtOfficePhon);

    email = (EditText) view. findViewById(R.id.edtEmail);
    skype = (EditText) view. findViewById(R.id.edtSkype);
    fax = (EditText) view. findViewById(R.id.edtFax);
    leadSource = (EditText) view. findViewById(R.id.edtLeadSource);
    target = (EditText) view. findViewById(R.id.edtTarget);
    leadCat = (EditText) view. findViewById(R.id.edtLeadCategory);
    contAdd = (EditText) view. findViewById(R.id.edtContactAddress);
    revenue = (EditText) view. findViewById(R.id.edtRev);
    revPoten = (EditText) view. findViewById(R.id.edtRevPot);
    mobile = (EditText) view. findViewById(R.id.edtMobile);
    saveMe = (Button) view. findViewById(R.id.btnSave);

    mydb=new DBHelper(view.getContext(),null,null,1);

    saveMe.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DetailClass details = new DetailClass("");
            details.setFirName(firName.getText().toString());
            details.setMiddName(middName.getText().toString());
            details.setLastName(lastName.getText().toString());
            details.setTitl(title.getText().toString());
            details.setSubTitle(subTitle.getText().toString());
            details.setDepartment(department.getText().toString());
            details.setDesignation(designation.getText().toString());
            details.setLevel(level.getText().toString());
            details.setReporTo(reporTo.getText().toString());
            details.setJobGrade(jobGrade.getText().toString());
            details.setMobile(mobile.getText().toString());
            details.setOfficePhone(officePhone.getText().toString());
            details.setEmail(email.getText().toString());
            details.setSkype(skype.getText().toString());
            details.setFax(fax.getText().toString());
            details.setLeadSource(leadSource.getText().toString());
            details.setTarget(target.getText().toString());
            details.setLeadCat(leadCat.getText().toString());
            details.setRevenue(revenue.getText().toString());
            details.setRevPoten(revPoten.getText().toString());
            details.setContAdd(contAdd.getText().toString());

            boolean getData = mydb.addLeadData(details);
            if (getData == true) {
            Toast.makeText(getActivity.this,"row inserted",Toast.Length_Long).show();
                getActivity().finish();
            }
        }
    });

}

Similarly, I have done for the third fragment. But i want to save all the data at one click from third fragment.How can i do this. please help.This is my detail class.

 public void setCompName(String compName) { this.compName = compName;}
public void setParentComp(String parentComp) {this.parentComp = parentComp;    }
public void setSpecilities(String specilities) {this.specilities = specilities;}
public void setGlobHunt(String globHuntClient) {this.globHuntClient = globHuntClient;}
public void setComptition(String comptition) {this.comptition = comptition;}
public void setnoPoach(String noPoach) {this.noPoach = noPoach;}
public void setblackList(String blackList) {this.blackList = blackList;}
public void setboardNo(String boardNo) {this.boardNo = boardNo;}
public void setindustry(String industry) {this.industry = industry;}
public void setindustryClassi(String indusClassification) {this.indusClassification = indusClassification;}
public void setindustrySubClassi(String induSubClassi) {this.induSubClassi = induSubClassi;}
public void settype(String type) {this.type = type;}
public void setwebsite(String website) {this.website = website;}
public void setproduct(String product) {this.product = product;}
public void setService(String service) {this.service = service;}
public void setToolsAndTech(String toolsAndTech) {this.toolsAndTech = toolsAndTech;}
public void setInterRecTeam(String interRecTeam) {this.interRecTeam = interRecTeam;}
public void setRevePotiPeriod(String revePotiPeriod) {this.revePotiPeriod = revePotiPeriod;}
public void setAccountCategory(String accountCategory) {this.accountCategory = accountCategory;}
public void setProcess(String process) {this.process = process;}


public void setFirName(String firName) { this.firName = firName;}
public void setMiddName(String middName) {this.middName = middName;    }
public void setLastName(String lastName) {this.lastName = lastName;}
public void setTitl(String titl) {this.titl = titl;}
public void setSubTitle(String subTitle) {this.subTitle = subTitle;}
public void setDepartment(String department) {this.department = department;}
public void setDesignation(String designation) {this.designation = designation;}
public void setLevel(String level) {this.level = level;}
public void setReporTo(String reporTo) {this.reporTo = reporTo;}
public void setJobGrade(String jobGrade) {this.jobGrade = jobGrade;}
public void setMobile(String mobile) {this.mobile = mobile;}
public void setOfficePhone(String officePhone) {this.officePhone = officePhone;}
public void setEmail(String email) {this.email = email;}
public void setSkype(String skype) {this.skype = skype;}
public void setFax(String fax) {this.fax = fax;}
public void setLeadSource(String leadSource) {this.leadSource = leadSource;}
public void setTarget(String target) {this.target = target;}
public void setLeadCat(String leadCat) {this.leadCat = leadCat;}
public void setContAdd(String contAdd) {this.contAdd = contAdd;}
public void setRevenue(String revenue) {this.revenue = revenue;}
public void setRevPoten(String revPoten) {this.revPoten = revPoten;}


public void setModeOfBusin(String modeOfBusin) { this.modeOfBusin = modeOfBusin;}
public void setProdComp(String prodComp) {this.prodComp = prodComp;    }
public void setLivePosiOnCarrer(String livePosiOnCarrer) {this.livePosiOnCarrer = livePosiOnCarrer;}
public void setRecTeam(String recTeam) {this.recTeam = recTeam;}
public void setRecTeamSize(String recTeamSize) {this.recTeamSize = recTeamSize;}
public void setSrOpenClosed(String srOpenClosed) {this.srOpenClosed = srOpenClosed;}
public void setNoOfEmp(String noOfEmp) {this.noOfEmp = noOfEmp;}
public void setFounded(String founded) {this.founded = founded;}
public void setGlobalOffice(String globalOffice) {this.globalOffice = globalOffice;}
public void setTotalMandates(String totalMandates) {this.totalMandates = totalMandates;}
public void setInhouseAttrRate(String inhouseAttrRate) {this.inhouseAttrRate = inhouseAttrRate;}
public void setServi(String servi) {this.servi = servi;}
public void setToolsAndTec(String toolsAndTec) {this.toolsAndTec = toolsAndTec;}
public void setIntRecTeam(String intRecTeam) {this.intRecTeam = intRecTeam;}
public void setAnnualRevPosition(String annualRevPosition) {this.annualRevPosition = annualRevPosition;}
public void setRevPotential(String revPotential) {this.revPotiential = revPotential;}
public void setAccCategory(String accCategory) {this.accCategory = accCategory;}
public void setProc(String proc) {this.proc = proc;}


public int get_id() {
    return _id;
}

public String getCompName() {return compName;}
public String getParentComp() {return parentComp;}
public String getSpecilities() {return specilities;}
public String getGlobHunt() { return globHuntClient; }
public String getComptition() { return comptition; }
public String getNoPoach() { return noPoach; }
public String getBlackList() { return blackList; }
public String getBoardNo() { return boardNo; }
public String getIndustry() { return industry; }
public String getIndusClassification() { return indusClassification; }
public String getInduSubClassi() { return induSubClassi; }
public String getType() { return type; }
public String getWebsite() { return website; }
public String getProduct() { return product; }
public String getService() { return service; }
public String getToolsAndTech() { return toolsAndTech; }
public String getInterRecTeam() { return interRecTeam; }
public String getRevePotiPeriod() { return revePotiPeriod; }
public String getAccountCategory() { return accountCategory; }
public String getProcess() { return process; }


public String getFirName() {return firName;}
public String getMiddName() {return middName;}
public String getLastName() {return lastName;}
public String getTitl() { return titl; }
public String getSubTitle() { return subTitle; }
public String getDepartment() { return department; }
public String getDesignation() { return designation; }
public String getLevel() { return level; }
public String getReporTo() { return reporTo; }
public String getJobGrade() { return jobGrade; }
public String getMobile() { return mobile; }
public String getOfficePhone() { return officePhone; }
public String getEmail() { return email; }
public String getSkype() { return skype; }
public String getFax() { return fax; }
public String getLeadSource() { return leadSource; }
public String getTarget() { return target; }
public String getLeadCat() { return leadCat; }
public String getContAdd() { return contAdd; }
public String getRevenue() { return revenue; }
public String getRevPoten() { return revPoten; }


public String getModeOfBusin() {return modeOfBusin;}
public String getProdComp() {return prodComp;}
public String getLivePosiOnCarrer() {return livePosiOnCarrer;}
public String getRecTeam() { return recTeam; }
public String getRecTeamSize() { return recTeamSize; }
public String getSrOpenClosed() { return srOpenClosed; }
public String getNoOfEmp() { return noOfEmp; }
public String getFounded() { return founded; }
public String getGlobalOffice() { return globalOffice; }
public String getTotalMandates() { return totalMandates; }
public String getInhouseAttrRate() { return inhouseAttrRate; }
public String getServi() { return servi; }
public String getToolsAndTec() { return toolsAndTec; }
public String getIntRecTeam() { return intRecTeam; }
public String getAnnualRevPosition() { return annualRevPosition; }
public String getRevPotiential() { return revPotiential; }
public String getAccCategory() { return accCategory; }
public String getProc() { return proc; }

}

This is my Dbhelper class.

public class DBHelper extends SQLiteOpenHelper {

private HashMap hp;
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_COMPANY_NAME= "compName";
public static final String CONTACTS_COLUMN_PARENT_COMPANY = "parentComp";
public static final String CONTACTS_COLUMN_SPECILITIES = "specilities";
public static final String CONTACTS_COLUMN_GLOBAL_HUNT_CLIENT = "globHuntClient";
public static final String CONTACTS_COLUMN_COMPTITION = "comptition";
public static final String CONTACTS_COLUMN_NO_POACH =   "noPoach";
public static final String CONTACTS_COLUMN_BLACKLIST = "blackLIst";
public static final String CONTACTS_COLUMN_BOARD_NO = "boardNo";
public static final String CONTACTS_COLUMN_INDUSTRY = "industry";
public DBHelper(Context context, String name,     SQLiteDatabase.CursorFactory    factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db)
{
    String DATABASE_CREATE="CREATE TABLE IF NOT EXISTS "  +   DATABASE_TABLE + "("
            +CONTACTS_COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
            +CONTACTS_COLUMN_COMPANY_NAME +" TEXT,"
            +CONTACTS_COLUMN_PARENT_COMPANY+" TEXT,"
            +CONTACTS_COLUMN_SPECILITIES+" TEXT,"
            +CONTACTS_COLUMN_GLOBAL_HUNT_CLIENT+" TEXT,"
            +CONTACTS_COLUMN_COMPTITION+" TEXT,"
            +CONTACTS_COLUMN_NO_POACH+" TEXT,"
            +CONTACTS_COLUMN_BLACKLIST+" TEXT,"
            +CONTACTS_COLUMN_BOARD_NO+" TEXT,"+CONTACTS_COLUMN_INDUSTRY+"  TEXT,"
            +CONTACTS_COLUMN_INDUSTRY_CLASSFICATION+"   TEXT,"+CONTACTS_COLUMN_INDUSTRY_SUBCLASSFICATION+" TEXT,"
    Log.i(TAG, "Creating DataBase: " + DATABASE_CREATE);
    db.execSQL(DATABASE_CREATE);
}
public void onUpgrade(SQLiteDatabase db,int oldVesrion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
    onCreate(db);
}

 public boolean addLeadData(DetailClass detailClass) {
ContentValues values = new ContentValues();
values.put(CONTACTS_COLUMN_COMPANY_NAME, detailClass.getCompName());
values.put(CONTACTS_COLUMN_PARENT_COMPANY, detailClass.getParentComp());
values.put(CONTACTS_COLUMN_SPECILITIES, detailClass.getSpecilities());
values.put(CONTACTS_COLUMN_GLOBAL_HUNT_CLIENT, detailClass.getGlobHunt());
values.put(CONTACTS_COLUMN_COMPTITION, detailClass.getComptition());
values.put(CONTACTS_COLUMN_NO_POACH, detailClass.getNoPoach());
values.put(CONTACTS_COLUMN_BLACKLIST, detailClass.getBlackList());
values.put(CONTACTS_COLUMN_BOARD_NO, detailClass.getBoardNo());
values.put(CONTACTS_COLUMN_INDUSTRY, detailClass.getIndustry());
    SQLiteDatabase db = getWritableDatabase();
db.insert(DATABASE_TABLE, null, values);
Log.d(TAG, "Database Created" + values);
//    Toast.makeText(getClass(),"Data saved", Toast.LENGTH_SHORT).show();
if (values.size() > 0) {
    Log.d(TAG, "Data is saved Successfully");

    db.close();
    return true;

} else {
    Log.d(TAG, "Failed");
    db.close();
    return false;
}

} }

2 Answers2

0

Some tips:

One way:

Serialize the Object (DetailClass), so you can pass it easily from one class to another through Intent and finally save the object in your database.

NB: Serialize Object is not mandatory if you are not passing it from one class to another.

Other way (not best but will work)

Set data in your DetailClass Object and save it in some SharedPreference by the help of Gson on click of Next/Submit button in 1st and 2nd fragment. something like :

Taken from here :

SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);

For save

Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(myObject); // myObject - instance of DetailClass Object 
prefsEditor.putString("MyObject", json);
prefsEditor.commit();

For get

Gson gson = new Gson();
String json = mPrefs.getString("MyObject", "");
DetailClass Object  = gson.fromJson(json, DetailClass.class);

NB: You can download GSON lib from here

Finally save this object in your local DB in 3rd fragment and remove the SharedPreference after a success save.

Community
  • 1
  • 1
Ranjit
  • 5,130
  • 3
  • 30
  • 66
0

I did it in some other way. I created table on the first fragment and updated the table on second and third fragment. what i did is i disabled 2nd fragment button and third fragment button when i was on fragment 1. when i went to second fragment i disabled 1st and 3rd fragment button. i did the same for third fragment. in this way i achieved the goal.