我有一个疑问是使用 volley 从服务器获取记录并将其保存在 SQLite 中并通过查询 getlist 设置适配器但是它没有立即显示结果我需要返回并再次查看新更新的数据如何将记录插入 SQLite 后立即显示该记录让我发布我的代码:
下面是从服务器获取记录的 volley 代码:
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"));
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);
这是从 sqlite android 设置适配器的适配器:
List<ModelClass>listobj=new Arraylist<Modelclass>();
listobj=account_sf_db.list();
accountListAdapter = new AccountListAdapter(listobj, getApplicationContext());
recyclerView.setAdapter(accountListAdapter);
这样做的目的是处理离线功能,即使在离线期间我们也必须使用 SQLite 访问它。
最佳答案
在 Sqlite 中创建数据库表时,您需要添加一个 notifyChange
与数据库表关联的 URI。
这是我如何在我的代码中执行此操作的示例。
我必须像这样为我的数据库表声明一个 URI:
// These are just some constants
public static final String DB_NAME = "mydatabase";
private static final int DATABASE_VERSION = 1;
public static final String ApplicationPackage = "com.example.myapp";
public static final String DB_TABLE_ACCOUNTS = "accounts";
// This is the URI of the database table
public static final Uri DB_TABLE_ACCOUNTS_URI = Uri
.parse("sqlite://" + Constants.ApplicationPackage + "/" + DB_TABLE_ACCOUNTS);
现在,当我使用函数创建数据库表条目时,该函数如下所示。
public void createAccounts(Account mNewAccount) {
SQLiteDatabase db = null;
try {
DataBaseOpenHelper dOpenHelper;
dOpenHelper = new DataBaseOpenHelper(context,DB_NAME,DATABASE_VERSION);
ContentValues values = new ContentValues();
values.put("account_name", mNewAccount.getAccountName());
values.put("account_number", mNewAccount.getAccountNumber());
db = dOpenHelper.getWritableDatabase();
db.insert(DBConstants.DB_TABLE_ACCOUNTS, null, values);
// Here you notify the change when the database is updated
context.getContentResolver().notifyChange(DB_TABLE_ACCOUNTS_URI, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
现在是DataBaseOpenHelper.java
public class DataBaseOpenHelper extends SQLiteOpenHelper {
private int newVersion;
private String name;
public DataBaseOpenHelper(Context context, String name, int version) {
super(context, name, null, version);
this.newVersion = version;
this.name = name;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table if not exists "
+ DBConstants.DB_TABLE_ACCOUNTS
+ "(_id integer primary key autoincrement, account_number text not null, "
+ "account_name text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
现在,当您从 Activity
查询数据库表时或 Fragment
你需要registerContentOberver
在你的onCreateLoader
如果你使用 LoaderManager.LoaderCallBacks<Cursor>
像这样:
Cursor cursor = DataHelper.getInstance(getActivity()).getAccounts();
this.registerContentObserver(cursor, DB_TABLE_ACCOUNTS_URI);
因此您需要从 Sqlite 数据库中再次加载数据以重新初始化您的 listobj
当响应从服务器返回时。然后调用accountListAdapter.notifyDataSetChanged()
.
关于java - 如何从服务器检索值并将其保存在 sqlite 中并立即显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35909076/