mardi 29 décembre 2015

Inserting data into sqlite database and retrieving and it is getting lost when after application restart

In my ProfileOneFragment I am updating data in onTextChanged method, but when I restart and retrieve the data to display in TextView it displays inital values at the time of creation.

This is my DBHelper class

public class DBHelper extends SQLiteOpenHelper {

    SQLiteDatabase db=this.getReadableDatabase();
    public DBHelper(Context context) {
        super(context, "edentitydb", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS profile (" +
                "profileid NUMBER PRIMARY KEY," +
                "fname TEXT," +
                "lname TEXT," +
                "alias TEXT NULL," +
                "designation TEXT NULL," +
                "organization TEXT NULL," +
                "primary_number TEXT," +
                "secondary_number TEXT," +
                "email TEXT," +
                "fax TEXT NULL," +
                "office_address NULL," +
                "home_address NULL" +
                ")");
        db.execSQL("INSERT INTO profile values" +
                "(1,'fname','lname',null,null,null,'primary_number',null,'email',null,null,null)");
        db.execSQL("INSERT INTO profile values" +
                "(2,'fname','lname',null,null,null,'primary_number',null,'email',null,null,null)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //db.execSQL("DROP TABLE IF EXISTS profile");
        //onCreate(db);
    }

    public void insertFname(String fn,int profile)
    {
        db.rawQuery("UPDATE profile SET fname='"+fn+"' WHERE profileid="+profile,null);
    }

    public String getFname(int profile)
    {
        String query = " SELECT fname FROM profile WHERE profileid =" + profile;
        Cursor c = db.rawQuery(query,null);
        if (c != null && c.moveToFirst()) {
            String fn = c.getString(0);
            c.close();
            return fn;
        }
        else
            return null;
    }
    public void insertLname(String ln,int profile)
    {
        db.rawQuery("UPDATE profile SET lname='"+ln+"' WHERE profileid="+profile,null);
    }
    public String getLname(int profile)
    {
        String query = " SELECT lname FROM profile WHERE profileid =" + profile;
        Cursor c = db.rawQuery(query,null);
        if (c != null && c.moveToFirst()) {
            String ln = c.getString(0);
            c.close();
            return ln;
        }
        else
            return null;
    }
    public void insertAlias(String al,int profile)
    {
        db.rawQuery("UPDATE profile SET alias='"+al+"' WHERE profileid="+profile,null);
    }
    public String getAlias(int profile)
    {
        String query = " SELECT alias FROM profile WHERE profileid =" + profile;
        Cursor c = db.rawQuery(query,null);
        if (c != null && c.moveToFirst()) {
            String al = c.getString(0);
            c.close();
            return al;
        }
        else
            return null;
    }
    public void updateFName(String name,int profile){
        //db.rawQuery("UPDATE profile SET fname='" + name + "' WHERE profileid=1" , null);
    }
    public void updateLName(String name,int profile){
        //db.rawQuery("UPDATE profile SET lname='"+name+"' WHERE profileid=?",new String[]{profile+""});
    }
    public void updateAlias(String alias,int profile){
        //db.rawQuery("UPDATE profile SET alias='"+alias+"' WHERE profileid=?",new String[]{profile+""});
    }
    public void updateDesignation(String designation,int profile){
        db.rawQuery("UPDATE profile SET designation='"+designation+"' WHERE profileid="+profile,null);
    }
    public void updateOrganization(String organization,int profile){
        db.rawQuery("UPDATE profile SET organization='" + organization + "' WHERE profileid=" + profile, null);
    }
    public void updatePrimaryNumber(String number,int profile){
        db.rawQuery("UPDATE profile SET primary_number='" + number + "' WHERE profileid=" + profile, null);
    }
    public void updateScondaryNumber(String number,int profile){
        db.rawQuery("UPDATE profile SET secondary_number='" + number + "' WHERE profileid=" + profile, null);
    }
    public void updateEmail(String email,int profile){
        db.rawQuery("UPDATE profile SET email='" + email + "' WHERE profileid=" + profile, null);
    }
    public void updateFax(String fax,int profile){
        db.rawQuery("UPDATE profile SET fax='" + fax + "' WHERE profileid=" + profile, null);
    }
    public void updateOfficeAddress(String address,int profile){
        db.rawQuery("UPDATE profile SET office_address='" + address + "' WHERE profileid=" + profile, null);
    }
    public void updateHomeAddress(String address,int profile){
        db.rawQuery("UPDATE profile SET home_address='" + address + "' WHERE profileid=" + profile, null);
    }

    public String getProfileDetails(int profile,String field){
        String str="";
        Cursor cursor=db.query("profile",new String[]{field},"profileid="+profile,null,null,null,null);
        if(cursor.moveToFirst()){
            str=cursor.getString(cursor.getColumnIndex(field));
        }
        return str;
    }
}

And this is my Fragment to handle

public class ProfileOneFragment extends Fragment {

    public ProfileOneFragment(){

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    DBHelper db;
    Context context;
    Spinner spinner_salutation;
    ArrayAdapter<CharSequence> adapter;
    EditText fone_fname,fone_lname,fone_alias;
    ImageView fone_dp;
    ProgressBar fone_progressBar;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_profile_one, container, false);
        context=getContext();
        db=new DBHelper(context);
        fone_progressBar=(ProgressBar)view.findViewById(R.id.fone_progressBar);
        spinner_salutation=(Spinner)view.findViewById(R.id.spinner_salutation);
        adapter=ArrayAdapter.createFromResource(context,R.array.salutations,R.layout.support_simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        spinner_salutation.setAdapter(adapter);
        spinner_salutation.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(context, parent.getItemAtPosition(position) + "", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        fone_dp=(ImageView)view.findViewById(R.id.fone_dp);
        fone_dp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        fone_fname=(EditText)view.findViewById(R.id.fone_fname);
        fone_lname=(EditText)view.findViewById(R.id.fone_lname);
        fone_alias=(EditText)view.findViewById(R.id.fone_alias);

        String temp = db.getFname(1);
        fone_fname.setText(temp);

        String temp1 = db.getLname(1);
        fone_lname.setText(temp1);

        String temp2 = db.getAlias(1);
        fone_alias.setText(temp2);

        fone_fname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {




            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                String fn = fone_fname.toString();
                db.insertFname(fn,1);
                Toast.makeText(getActivity(),"Saved Successfully", Toast.LENGTH_LONG).show();

            }

            @Override
            public void afterTextChanged(Editable s) {
                db.updateFName(s.toString(),1);
            }
        });
        fone_lname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                String ln = fone_lname.toString();
                db.insertLname(ln,1);
                Toast.makeText(getActivity(),"Saved Successfully", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void afterTextChanged(Editable s) {
                db.updateLName(s.toString(), 1);
            }
        });
        fone_alias.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                String al = fone_alias.toString();
                db.insertAlias(al,1);
                Toast.makeText(getActivity(),"Saved Successfully", Toast.LENGTH_SHORT).show();


            }

            @Override
            public void afterTextChanged(Editable s) {
                db.updateAlias(s.toString(), 1);
            }
        });




        return view;
    }

}

Aucun commentaire:

Enregistrer un commentaire