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 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);//通过server获取数据
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
notifyDataSetChanged()刷新ListView(使用JSONArray绑定的Adapter)的更多相关文章
- 关于ListView中notifyDataSetChanged()刷新数据不更新原因
使用Listview的时候: 当要动态显示更改后的数据(例如数据库改动), 很多人应该都用过notifyDataSetChanged();这个方法来刷新Listview,显示改后的数据. 这时候就要注 ...
- 滚动到底部加载更多及下拉刷新listview的使用
最新内容建议直接访问原文:滚动到底部加载更多及下拉刷新listview的使用 本文主要介绍可同时实现下拉刷新及滑动到底部加载更多的ListView的使用. 该ListView优点包括:a. 可自定义下 ...
- RecyclerView加载更多用notifyDataSetChanged()刷新图片闪烁
首先来看看对比ListView看一下RecyclerView的Adapter主要增加了哪些方法: notifyItemChanged(int position) 更新列表position位置上的数据可 ...
- RecyclerView中notifyDataSetChanged刷新总结
除了adapter.notifyDataSetChanged()这个方法之外,新的Adapter还提供了其他的方法,如下: public final void notifyDataSetChanged ...
- ListView控件绑定DataSet
DataSet数据集,数据缓存在客户端内存中,支持断开式连接. 在对DataSet做操作的时候,首先一定要修改其行的状态,然后执行SqlDataAdapter的Update方法,Update方法根 ...
- 关于调用notifyDataSetChanged刷新PullToRefreshListView列表无反应解决办法
文章转载自:关于调用notifyDataSetChanged刷新PullToRefreshListView列表无反应解决办法 | TeachCourse
- ListView:The content of the adapter has changed but ListView did not receive a notification终极解决方法
使用ListView时遇到如下的异常信息: 10-26 18:30:45.085: E/AndroidRuntime(7323): java.lang.IllegalStateException: T ...
- 自定义控件学习——下拉刷新ListView
效果 开始用Android Studio写了,还有挺多不明白这IDE用法的地方....蛋疼 主要思路 1. 添加了自定义的头布局 2. 默认让头布局隐藏setPadding.设置 -自身的高度 ...
- Android 自定义下拉刷新ListView
package com.dwtedx.qq.view; import android.content.Context; import android.util.AttributeSet; import ...
随机推荐
- 3Ddungeon-------三维搜索-----偷个懒 把 亡命逃窜 的代码修改了一下 拿来用了
题 很简单 就是给一个 三维的迷宫然后 开你起始地点 S 问你能不能到达 出口 E 能的话 需要多长时间 ? #include<stdio.h> #include<string ...
- F - Micro-World(简单模拟)
Problem description You have a Petri dish with bacteria and you are preparing to dive into the harsh ...
- C#之考勤系统
闲来无聊,搞搞C#,下面就是我写的一个Demo 员工类 using System; using System.Collections.Generic; using System.Linq; using ...
- 修改Switch 的颜色
1:效果图 2:布局 <Switch android:id="@+id/switch_bg" style="@style/switchStyle" and ...
- WPF度量系統
和Winform不同,WPF的度量單位不是像素,而是設備無關單位DIU,其大小總是1/96吋 那麽,WPF中一個寬度爲96的按鈕,到底是多少個像素呢? 答:取決於系統DPI. 計算公式爲:實際像素 = ...
- 【sqli-labs】 less17 POST - Update Query- Error Based - String (基于错误的更新查询POST注入)
这是一个重置密码界面,查看源码可以看到username作了防注入处理 逻辑是先通过用户名查出数据,在进行密码的update操作 所以要先知道用户名,实际情况中可以注册用户然后实行攻击,这里先用admi ...
- X.509 certificate
A digital certificate is a collection of data used to securely distribute the public half of a publi ...
- HTML input 控件
<input type="file" id="file1" onChange="test()"> function test() ...
- git_安装与配置
安装 windows平台安装 在windows平台安装git,需要下载exe.文件,下载地址:https://gitforwindows.org/,双击下载的.exe文件,按照提示进行安装,安装完成后 ...
- 定位IO瓶颈的方法,iowait低,IO就没有到瓶颈?
通过分析mpstat的iowait和iostat的util%,判断IO瓶颈 IO瓶颈往往是我们可能会忽略的地方(我们常会看top.free.netstat等等,但经常会忽略IO的负载情况),今天给大家 ...