notifyDataSetChanged()刷新ListView(使用JSONArray绑定的Adapter)
来源:互联网 发布:centos6.5图形网络配置 编辑:程序博客网 时间:2024/06/10 19:35
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;
}
}
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;
}
}
0 0
- notifyDataSetChanged()刷新ListView(使用JSONArray绑定的Adapter)
- Android listView使用Adapter.notifyDataSetChanged()不刷新
- Android listView 的适配器adapter.notifyDataSetChanged();刷新没有作用
- ListView刷新:自定义adapter 调用notifyDatasetChanged()方法不起作用 的解决方案
- Android ListView Adapter notifyDataSetChanged 刷新没有效果
- Adapter调用notifyDataSetChanged后listview不刷新
- android ListView adapter notifyDataSetChanged刷新先决条件
- Android ListView Adapter notifyDataSetChanged 刷新没有效果
- 使用adapter的notifyDataSetChanged时列表数据没有刷新
- android listview用adapter.notifyDataSetChanged()无法刷新每项的图标
- adapter.notifyDataSetChanged()无刷新
- adapter.notifyDataSetChanged()刷新不起作用
- listView使用simpleAdapter通过notifyDataSetChanged()方法刷新数据问题
- ListView adapter notifyDataSetChanged() track
- adapter的notifyDataSetChanged()无效
- 如何动态刷新ListView的显示---notifyDataSetChanged
- listview notifydatasetchanged 不刷新的一种解释
- android使用notifyDataSetChanged()方法,adapter的数据更新了,但是ListView的内容没有更新;
- pyrDown和pyrUp函数
- 今日笔记之 页面跳转 为tableview添加手势关闭键盘 textview获取焦点
- POI解决EXCLE导入03,07兼容性问题
- iOS ARC也会有内存泄露
- mongodb创建集合
- notifyDataSetChanged()刷新ListView(使用JSONArray绑定的Adapter)
- 2015年多校训练总结
- jsapi微信支付v3版
- 数据挖掘学习路线
- Deck
- Android之ExpandableListView的属性(Group不展开)
- [置顶] VS2010中文注释带红色下划线的解决方法
- 系统日志Logrotate配置
- 【算法】Sky Map