码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-08-21 11:26:39      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:android

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 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;
 }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:android

原文地址:http://blog.csdn.net/ahhmdwg/article/details/47829875

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!