0

I have read that content provider is needed for update responsive changes in sqlitedb . I have made application with sqliteonehelper class where within this i have written all queries now i need to make responsive UI whenever changes made in sqlite db UI need to updated with updated value i have read that i can be done with Loader class but it need content provider i think so but i don't like to user provider for my app how can i implement without provider let me tell you what i have so far:

This is sqliteonehelper class where i writted query:

 public void InsertorUpdate(Model_Account modlobj){
    SQLiteDatabase db=this.getReadableDatabase();
    String query=" SELECT  * FROM " + Model_Account.Accunt_Table+ " WHERE " + Model_Account.Account_ID +  "  =" +  modlobj.getAccountID() +"";
    Cursor c = db.rawQuery(query, null);
    if (c.moveToFirst()) {
        Update(modlobj);
    } else {
        onInsert(modlobj);
    }db.close();
}    

This is were am implementing in my activity

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_sf_db.InsertorUpdate(modelobjs);

                    }
                }  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);    

List in sqliteonehelper class:

 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!=null && cursor.getCount()>0) {
            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 where am setting adapter to my recyclerview in activity:

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

am bit confused where to start with can anyone helpme out Thanks in advance!!

Yog
  • 137
  • 2
  • 14

1 Answers1

0

you can refer this link which creates custom loader and reads data directly from sqlite database without using content provider.

Chirag Chavda
  • 556
  • 4
  • 12