dimanche 13 mars 2016

How can i make responsive Sqlite Database with android?

Let me say my requirement whenever i insert the some value in sqlitedatabase i need to populate recyclerview instantly how can i do this !! well i have read i can be done through contentprovider or loadercallback already i have done half the project with sqliteonehelper class where i have done all query according to my requirement only thing missing is instantly i could not populate recyclerview how can i do this am bit confused with contentprovider ,loadercallback, content observer i have read in one post i cannot be achieved without contentprovider do i really need provider for responsive changes in db or can it be achieve without this ! let me post what i have done so far:

This is sqliteonehelper class:

public class Account_DB extends SQLiteOpenHelper {
        private static final int DATABASE_VERSION = 4;
        private static final String DATABASE_NAME = "account.db";
        Context mcontext;
        SQLiteDatabase db;
        public static final Uri DB_TABLE_ACCOUNTS_URI = Uri
                .parse("sqlite://" + "xx" + "/" + DATABASE_NAME);
        public Account_SF_DB(android.content.Context context) {

            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            this.mcontext=context;
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            String CREATE_TABLE_ACCOUNT = "CREATE TABLE " + Model_Account.Accunt_Table + "("
                    + Model_Account.id + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
                    + Model_Account.company_groups + " TEXT, "
                    + Model_Account.Parent_company + " TEXT, "
                    + Model_Account.Company_name + " TEXT, "
                    + Model_Account.Company_type + " TEXT ,"
                    + Model_Account.Address_line1 + " TEXT ,"
                    + Model_Account.Address_line2 + " TEXT ,"
                    + Model_Account.Address_line3 + " TEXT ,"
                    + Model_Account.State + " TEXT ,"
                    + Model_Account.State_id + " INTEGER ,"
                    + Model_Account.City + " TEXT ,"
                    + Model_Account.CityID + " INTEGER ,"
                    + Model_Account.pincode + " TEXT ,"
                    + Model_Account.Landline1 + " TEXT ,"
                    + Model_Account.Landline2 + " TEXT ,"
                    + Model_Account.Url + " TEXT ,"
                    + Model_Account.Email_id + " TEXT ,"
                    + Model_Account.Industry + " TEXT ,"
                    + Model_Account.IndustryID + " INTEGER ,"
                    + Model_Account.companyname_id + " INTEGER ,"
                    + Model_Account.Account_manager + " TEXT ,"
                    + Model_Account.Account_managerid + " INTEGER ,"
                    + Model_Account.Region + " TEXT ,"
                    + Model_Account.regionid + " INTEGER , "
                    + Model_Account.Muti_location + " TEXT ,"
                    + Model_Account.mutilocationid + " INTEGER , "
                    + Model_Account.spinner_state_position + " INTEGER ,"
                    + Model_Account.parent_company_spinner_pos + " INTEGER ,"
                    + Model_Account.city_spinner_pos + " INTEGER ,"
                    + Model_Account.industry_spinner_pos + " INTEGER ,"
                    + Model_Account.acm_spinner_pos + " INTEGER ,"
                    + Model_Account.region_spinner_pos + " INTEGER ,"
                    + Model_Account.Account_ID + " INTEGER"
                    + ");";

            db.execSQL(CREATE_TABLE_ACCOUNT);


        }

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

        public void onInsert(Model_Account accountobj) {
            db = this.getWritableDatabase();
            ContentValues contentobj = new ContentValues();
            contentobj.put(Model_Account.company_groups, accountobj.getCompany_group());
            contentobj.put(Model_Account.Parent_company, accountobj.getParent_company());
            contentobj.put(Model_Account.Company_name, accountobj.getCompany_name());
            contentobj.put(Model_Account.Company_type, accountobj.getCompany_type());
            contentobj.put(Model_Account.Address_line1, accountobj.getAddrss_line1());
            contentobj.put(Model_Account.Address_line2, accountobj.getAddrss_line2());
            contentobj.put(Model_Account.Address_line3, accountobj.getAddrss_line3());
            contentobj.put(Model_Account.State, accountobj.getState());
            contentobj.put(Model_Account.State_id, accountobj.getStateid());
            contentobj.put(Model_Account.City, accountobj.getCity());
            contentobj.put(Model_Account.CityID, accountobj.getCityid());
            contentobj.put(Model_Account.pincode, accountobj.getPincode());
            contentobj.put(Model_Account.Landline1, accountobj.getLandline1());
            contentobj.put(Model_Account.Landline2, accountobj.getLandline2());
            contentobj.put(Model_Account.Url, accountobj.getUrl());
            contentobj.put(Model_Account.Email_id, accountobj.getEmailid());
            contentobj.put(Model_Account.Industry, accountobj.getIndusty());
            contentobj.put(Model_Account.IndustryID, accountobj.getIndustryid());
            contentobj.put(Model_Account.Account_manager, accountobj.getAccount_manager());
            contentobj.put(Model_Account.Account_managerid, accountobj.getAccountmanager());
            contentobj.put(Model_Account.Region, accountobj.getRegion());
            contentobj.put(Model_Account.regionid, accountobj.getRegionID());
            contentobj.put(Model_Account.Muti_location, accountobj.getMulti_location());
            contentobj.put(Model_Account.mutilocationid, accountobj.getMulti_location());
            contentobj.put(Model_Account.companyname_id, accountobj.getCompanyname());
            contentobj.put(Model_Account.Account_ID, accountobj.getAccountID());
            contentobj.put(Model_Account.spinner_state_position,accountobj.getSpinner_state_pos());
            contentobj.put(Model_Account.parent_company_spinner_pos,accountobj.getParent_company_pos());
            contentobj.put(Model_Account.city_spinner_pos,accountobj.getCity_pos());
            contentobj.put(Model_Account.industry_spinner_pos,accountobj.getIndustry_pos());
            contentobj.put(Model_Account.acm_spinner_pos,accountobj.getAcm_spinner_position());
            contentobj.put(Model_Account.region_spinner_pos, accountobj.getRegion_spinner_posion());
            db.insert(Model_Account.Accunt_Table, null, contentobj);
              db.close();
        }
  public List<Model_Account> list() {
        String countQuery = "SELECT  * FROM " + Model_Account.Accunt_Table;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        List<Model_Account> listobj = new ArrayList<Model_Account>();
        if (cursor.moveToFirst()) {
            do {
                Model_Account modelobj = new Model_Account();
                modelobj.setID(cursor.getInt(cursor.getColumnIndex(Model_Account.id)));
                modelobj.setCompany_group(cursor.getString(cursor.getColumnIndex(Model_Account.company_groups)));
                modelobj.setState(cursor.getString(cursor.getColumnIndex(Model_Account.State)));

                listobj.add(modelobj);

            } while (cursor.moveToNext());
        }

        return listobj;


    }

This is the activity where am implementing this insert method:

@Override
    protected Void doInBackground(String... params) {
        RequestQueue queue = Volley.newRequestQueue(getBaseContext());


        JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Request.Method.GET, params[0], new JSONObject(),
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        String server_response = response.toString();
                        try {
                            JSONObject json_object = new JSONObject(server_response);
                            JSONArray json_array = new JSONArray(json_object.getString("AccountPageLoadAccountListResult"));
                            for (int i = 0; i < json_array.length(); i++) {
                                Model_Account modelobjs = new Model_Account();
                                JSONObject json_arrayJSONObject = json_array.getJSONObject(i);
                                modelobjs.setCompany_group(json_arrayJSONObject.getString("CompanyName"));
                                modelobjs.setState(json_arrayJSONObject.getString("Region"));
                                modelobjs.setAccountID(json_arrayJSONObject.getInt("AccountID"));
                                account_xx_db.InsertorUpdate(modelobjs);
                                accountListAdapter.notifyDataSetChanged();



                            }
                        }  catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getBaseContext(), error.toString(), Toast.LENGTH_SHORT).show();

                    }
                });
        queue.add(jsonObjRequest);
       return null;
    }

This is where am setting list function from sqliteonehelper class to recyclerview adapter:

  listobj = account_sf_db.list();
                accountListAdapter = new AccountListAdapter(listobj, getApplicationContext());
                recyclerView.setAdapter(accountListAdapter);

But i could not get instantly i need to go back and come again to that activity to see that newly updated value how can i get it instantly after i insert into db this is problem am facing can somebody tell me the solution for this!!

Aucun commentaire:

Enregistrer un commentaire