首页 > 代码库 > notifyDataSetChanged()刷新ListView(使用JSONArray绑定的Adapter)

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, Helvetica, sans-serif;">; 
  } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  handler.sendMessage(msg);
  }
 }).start();
 }
 @Override
 public void onl oad() {
 // 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);//通过server获取数据
 } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }




 return result;
 }
}

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