2016-03-15 21 views
0

我已经读过需要内容提供者来更新sqlitedb响应式更改。我已经使用sqliteonehelper类的应用程序,在这里我已经写了所有的查询,现在我需要做出响应式的UI,只要在sqlite数据库用户界面中所做的更改需要更新更新的值我已阅读,我可以用Loader类来完成,但它需要内容供应商我想是这样,但我不喜欢的用户提供了我的应用程序如何可以实现不提供让我告诉你我到目前为止什么:如何在没有内容提供者的情况下实现Loader?

这是sqliteonehelper类在那里我创作的查询:

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

这是我正在执行的活动

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

名单sqliteonehelper类:

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; 

这是我在活动设置适配器我recyclerview:

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

上午有点困惑从哪里开始的任何人都可以提前helpme了谢谢!

回答

0

你可以参考这个link它创建自定义加载器,并直接从sqlite数据库中读取数据,而无需使用内容提供者。

相关问题