Android 联机时更新listview内容
我正在使用截取从json获取数据,我想将数据添加到缓存中。我添加到缓存中,它在脱机模式下工作正常,但在联机模式下,由于从json api获取数据,每个数据都会加倍。所以我的问题是,在获取json数据并显示新数据并用新数据更新缓存之前,如何向用户显示缓存数据 下面是我读取缓存数据和获取联机数据的代码:Android 联机时更新listview内容,android,json,listview,caching,Android,Json,Listview,Caching,我正在使用截取从json获取数据,我想将数据添加到缓存中。我添加到缓存中,它在脱机模式下工作正常,但在联机模式下,由于从json api获取数据,每个数据都会加倍。所以我的问题是,在获取json数据并显示新数据并用新数据更新缓存之前,如何向用户显示缓存数据 下面是我读取缓存数据和获取联机数据的代码: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_tab1, container, false);
list1=(ListView)v.findViewById(R.id.kunlist);
list2=(ListView)v.findViewById(R.id.oylist);
list3=(ListView)v.findViewById(R.id.haflist);
listitem=new ArrayList<>();
itemList2=new ArrayList<>();
itemList3=new ArrayList<>();
loadfirst();
loadingView();
// Inflate the layout for this fragment
return v;
}
private void loadingView() {
StringRequest request=new StringRequest(Request.Method.GET, URL_Data,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject=new JSONObject(response);
JSONObject array=jsonObject.getJSONObject("data");
ObjectOutput out=new ObjectOutputStream(new FileOutputStream(new File(activity2.getCacheDir(),"")+File.separator+"cache.srl"));
out.writeObject(array.toString());
out.close();
jsonRead(array);
} catch (JSONException e) {
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Activity activity = getActivity();
if(activity != null && isAdded()&&getContext()!=null)
Toast.makeText(getContext(),"Internet problem",Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue= Volley.newRequestQueue(context);
requestQueue.add(request);
}
public void loadfirst(){
try{
ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File(activity2.getCacheDir()+File.separator+"cache.srl")));
JSONObject jObject=new JSONObject((String)in.readObject());
in.close();
jsonRead(jObject);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
public void jsonRead(JSONObject obj) throws JSONException, UnsupportedEncodingException {
JSONArray day=obj.getJSONArray("day");
JSONArray week=obj.getJSONArray("week");
JSONArray month=obj.getJSONArray("month");
KunItem item=null;
KunItem kun=null;
KunItem hafta=null;
for(int i=0;i<day.length();i++) {
JSONObject o = day.getJSONObject(i);
if (Tab1.this.res.getConfiguration().locale.getLanguage().equals("uz")) {
item = new KunItem(
o.getString("name_uz"),
o.getString("price"),
o.getString("activation_code")
);
} else if (Tab1.this.res.getConfiguration().locale.getLanguage().equals("ru")) {
item = new KunItem(
URLDecoder.decode(URLEncoder.encode(o.getString("name_ru"), "iso8859-1"), "UTF-8"),
o.getString("price"),
o.getString("activation_code"));
}
listitem.add(item);
adapter = new KunAdapter(context, listitem);
list1.setAdapter(adapter);
}
for(int i=0;i<week.length();i++) {
JSONObject o = week.getJSONObject(i);
if (Tab1.this.res.getConfiguration().locale.getLanguage().equals("uz")) {
kun = new KunItem(
o.getString("name_uz"),
o.getString("price"),
o.getString("activation_code")
);
}
else if (Tab1.this.res.getConfiguration().locale.getLanguage().equals("ru")) {
kun = new KunItem(
URLDecoder.decode(URLEncoder.encode(o.getString("name_ru"), "iso8859-1"), "UTF-8"),
o.getString("price"),
o.getString("activation_code"));
}
itemList2.add(kun);
adapter2 = new KunAdapter(context, itemList2);
list3.setAdapter(adapter2);
}
for(int i=0;i<month.length();i++) {
JSONObject o = month.getJSONObject(i);
if (Tab1.this.res.getConfiguration().locale.getLanguage().equals("uz")) {
hafta = new KunItem(
o.getString("name_uz"),
o.getString("price"),
o.getString("activation_code")
);
} else if (Tab1.this.res.getConfiguration().locale.getLanguage().equals("ru")) {
hafta = new KunItem(
URLDecoder.decode(URLEncoder.encode(o.getString("name_ru"), "iso8859-1"), "UTF-8"),
o.getString("price"),
o.getString("activation_code")
);
}
itemList3.add(hafta);
adapter3=new KunAdapter(context,itemList3);
list2.setAdapter(adapter3);
}
}
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图v=充气机。充气(R.layout.fragment_tab1,容器,假);
list1=(ListView)v.findViewById(R.id.kunlist);
list2=(ListView)v.findViewById(R.id.oylist);
list3=(ListView)v.findViewById(R.id.haflist);
listitem=新的ArrayList();
itemList2=新的ArrayList();
itemList3=新的ArrayList();
loadfirst();
加载视图();
//为该碎片膨胀布局
返回v;
}
私有void加载视图(){
StringRequest=新的StringRequest(request.Method.GET、URL\u数据、,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject JSONObject=新JSONObject(响应);
JSONObject数组=JSONObject.getJSONObject(“数据”);
ObjectOutput out=新的ObjectOutputStream(新文件(activity2.getCacheDir(),“”)+File.separator+“cache.srl”);
out.writeObject(array.toString());
out.close();
jsonRead(数组);
}捕获(JSONException e){
Toast.makeText(getContext(),e.getMessage(),Toast.LENGTH_LONG.show();
e、 printStackTrace();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Activity=getActivity();
if(活动!=null&&isAdded()&&getContext()!=null)
Toast.makeText(getContext(),“互联网问题”,Toast.LENGTH_SHORT.show();
}
});
RequestQueue RequestQueue=Volley.newRequestQueue(上下文);
添加(请求);
}
公共void loadfirst(){
试一试{
ObjectInputStream in=新ObjectInputStream(新文件InputStream(新文件(activity2.getCacheDir()+File.separator+“cache.srl”));
JSONObject jObject=新的JSONObject(.readObject()中的字符串);
in.close();
jsonRead(jObject);
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(StreamCorruptedException e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}catch(classnotfounde异常){
e、 printStackTrace();
}捕获(JSONException e){
e、 printStackTrace();
}
}
public void jsonRead(JSONObject obj)抛出JSONException,UnsupportedEncodingException{
JSONArray day=obj.getJSONArray(“day”);
JSONArray week=obj.getJSONArray(“周”);
JSONArray month=obj.getJSONArray(“month”);
KunItem=null;
KunItem kun=null;
KunItem hafta=null;
对于(inti=0;i你必须这样做
1-通过内部数据库中的数据显示listview(如果有)
2-从服务器获取新数据
3-清除内部数据库中以前的数据(如果成功完成)
4-在内部数据库中保存新数据
5-使用listview向用户显示获取的数据是的,你说得对!我已经这样做了,我在联机时无法在listview中显示数据,它同时显示脱机数据和从服务器获取的数据)。您对此有什么建议吗!还有一个问题:如何删除内部数据库中以前的数据?是的,当您从服务器获取新数据时,请先清除列表,然后填写列表并通知DataSetChange适配器。这很有帮助!