当没有互联网连接(数据来自服务器)时,我如何存储列表项



关于
我有一个列表视图,包含4项,其中3项是文本,第四项是图像所有数据都在json中的服务器上代码工作正常,但当互联网关闭时,所有项目和列表都不显示如何使列表在有和没有互联网连接的情况下工作因为我每天都会向数据库中添加新项目这是我的代码

requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("allstudents");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject respons = jsonArray.getJSONObject(i);
String id = respons.getString("id");
String name = respons.getString("name");
String info = respons.getString("info");
String img = respons.getString("img");
link = respons.getString("link");
voicelink = respons.getString("voicelink");
listitmes.add(new listitme(id, name, info, img, link, voicelink));
allitems();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", "ERROR");
}
}
);
requestQueue.add(jsonObjectRequest);
}

public void allitems() {
listAdapter lsadapter = new listAdapter(listitmes);
listView.setAdapter(lsadapter);
}

任何解决方案我找了很多,但没有任何答案我在StackOverflow上找不到任何类似的东西

查看https://developer.android.com/reference/android/content/SharedPreferences.

使用Shared SharedPreferences,您可以保存列表,并在没有wifi的情况下调用它!

您可以做的是:

  • 从Internet接收数据后,立即将下载的数据保存到文件(image(/属性(text。((
  • 获取数据时在活动中显示消息(例如上次同步:时间戳(
  • 如果无法连接到Internet以获取新数据,请从文件/属性加载数据
  • 如果您无法连接到Internet并且没有任何保存的数据,请显示一条消息(例如,无法连接到互联网-可能会将其涂上颜色以突出显示这是一个错误(

我的2美分。。。

最新更新