让我说出我的要求每当我在sqlitedatabase中插入一些值时,我需要立即填充recyclerview我该怎么做!!好吧,我已经读过我可以通过contentprovider或loadercallback完成已经用sqliteonehelper类完成了一半的项目,我根据我的要求完成了所有查询只有丢失的东西是我无法填充recyclerview我怎么能这样做有点困惑与contentprovider,loadercallback,内容观察者我已经在一篇文章中读到了我无法实现没有contentprovider我真的需要提供者在数据库中进行响应式更改,或者可以实现没有这个!让我发布到目前为止我所做的事情:
这是sqliteonehelper类:
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;
}
这是实现此插入方法的活动:
@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;
}
这是从sqliteonehelper类到recyclerview适配器设置列表功能的地方:
listobj = account_sf_db.list();
accountListAdapter = new AccountListAdapter(listobj, getApplicationContext());
recyclerView.setAdapter(accountListAdapter);
但是我无法立即得到我需要回去再次参加那个活动,看看新更新的价值我怎样才能在插入数据库后立即得到它这是面临的问题可以有人告诉我这个解决方案!!
答案 0 :(得分:1)
你正在抛弃你的所有代码而且查看所有代码真的很难,但我可以建议的是创建一个像这样的方法
public void updateList(List<Model_Account> listobj){
accountListAdapter = new AccountListAdapter(listobj, getApplicationContext());
recyclerView.setAdapter(accountListAdapter);
accountListAdapter.notifyDataSetChanged();
}
然后在检索Json数据后调用此方法,这意味着在onResponse
内或者您希望在何处更新列表类似
@Override
public void onResponse(JSONObject response) {
//other Codes
updateList(listobj)
}