notifyDataSetChanged()刷新ListView使用JSONArray绑定的Adapter

1.fragment代码: package com.ts.fragment; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.example.mywinxintest.R; import com.ts.fragment.AutoListView; import com.ts.fragment.AutoListView.OnLoadListener; import com.ts.fragment.AutoListView.OnRefreshListener; import com.ts.myst.JSONArrayAdapter; import com.ts.util.HttpUtil; import android.annotation.SuppressLint; import android.graphics.Paint; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ListView; import android.widget.Spinner; import android.widget.TextView; public class SearchFragment extends Fragment implements OnRefreshListener,OnLoadListener { Spinner areaSpinner; TextView searchTextView; ListView itemListView; Map<String,String> countMap = new HashMap<String,String>(); String url1 = HttpUtil.BASE_URL+"asset_findAssetArea.action"; String url2 = HttpUtil.BASE_URL+"asset_findAssetSB.action"; String url3 = HttpUtil.BASE_URL+"asset_findAssetSBCount.action"; private AutoListView lstv; private JSONArrayAdapter jsonArrayAdapter; private JSONArray itemJsonArray = new JSONArray(); private String areaId; private Handler handler = new Handler(){ @SuppressLint("NewApi") public void handleMessage(Message msg) { String result = (String) msg.obj; JSONArray resultJsonArray = null; try { resultJsonArray = new JSONArray(result); } catch (JSONException e1) { // Todo Auto-generated catch block e1.printstacktrace(); } switch (msg.what) { case AutoListView.REFRESH: lstv.onRefreshComplete(); try { for (int i = 0; i < itemJsonArray.length(); i++) { itemJsonArray.remove(i); } for (int i = 0; i < resultJsonArray.length(); i++) { itemJsonArray.put(resultJsonArray.get(i)); } } catch (JSONException e) { // Todo Auto-generated catch block e.printstacktrace(); } break; case AutoListView.LOAD: lstv.onLoadComplete(); for (int i = 0; i < resultJsonArray.length(); i++) { try { itemJsonArray.put(resultJsonArray.get(i)); } catch (JSONException e) { // Todo Auto-generated catch block e.printstacktrace(); } } break; } lstv.setResultSize(resultJsonArray.length()); jsonArrayAdapter.notifyDataSetChanged(); }; }; @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { View v = inflater.inflate(R.layout.search_fragment,null) ; lstv = (AutoListView) v.findViewById(R.id.lstv); try{ itemJsonArray = new JSONArray("[]"); jsonArrayAdapter = new JSONArrayAdapter(getActivity(),itemJsonArray,"assetCode_assetName_assetSyr",false,"3"); // itemListView.setAdapter(adapter2); lstv.setAdapter(jsonArrayAdapter); lstv.setonRefreshListener(this); lstv.setonLoadListener(this); initData(areaId); @Override public void onnothingSelected(AdapterView<?> parent) { // Todo Auto-generated method stub } }); } catch(Exception e){ e.printstacktrace(); } return v ; } private void initData(String areaId) { loadData(AutoListView.REFRESH,areaId); } private void loadData(final int what,final String areaId) { new Thread(new Runnable() { @Override public void run() { // Todo Auto-generated method stub try { Thread.sleep(700); } catch (InterruptedException e) { e.printstacktrace(); } Message msg = handler.obtainMessage(); msg.what = what; try { msg.obj = <span style="font-family: Arial,Helvetica,sans-serif;">getData(areaId) <span style="font-family: Arial,sans-serif;">; } catch (Exception e) { // Todo Auto-generated catch block e.printstacktrace(); } handler.sendMessage(msg); } }).start(); } @Override public void onLoad() { // Todo Auto-generated method stub loadData(AutoListView.LOAD,areaId); } @Override public void onRefresh() { // Todo Auto-generated method stub loadData(AutoListView.REFRESH,areaId); } // 测试数据 public String getData(String areaId) { String result = null; try { result = HttpUtil.postRequest(url2,countMap);//通过服务器获取数据 } catch (Exception e) { // Todo Auto-generated catch block e.printstacktrace(); } return result; } }

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...