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)的更多相关文章

  1. 关于ListView中notifyDataSetChanged()刷新数据不更新原因

    使用Listview的时候: 当要动态显示更改后的数据(例如数据库改动), 很多人应该都用过notifyDataSetChanged();这个方法来刷新Listview,显示改后的数据. 这时候就要注 ...

  2. 滚动到底部加载更多及下拉刷新listview的使用

    最新内容建议直接访问原文:滚动到底部加载更多及下拉刷新listview的使用 本文主要介绍可同时实现下拉刷新及滑动到底部加载更多的ListView的使用. 该ListView优点包括:a. 可自定义下 ...

  3. RecyclerView加载更多用notifyDataSetChanged()刷新图片闪烁

    首先来看看对比ListView看一下RecyclerView的Adapter主要增加了哪些方法: notifyItemChanged(int position) 更新列表position位置上的数据可 ...

  4. RecyclerView中notifyDataSetChanged刷新总结

    除了adapter.notifyDataSetChanged()这个方法之外,新的Adapter还提供了其他的方法,如下: public final void notifyDataSetChanged ...

  5. ListView控件绑定DataSet

    DataSet数据集,数据缓存在客户端内存中,支持断开式连接.   在对DataSet做操作的时候,首先一定要修改其行的状态,然后执行SqlDataAdapter的Update方法,Update方法根 ...

  6. 关于调用notifyDataSetChanged刷新PullToRefreshListView列表无反应解决办法

    文章转载自:关于调用notifyDataSetChanged刷新PullToRefreshListView列表无反应解决办法 | TeachCourse

  7. 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 ...

  8. 自定义控件学习——下拉刷新ListView

    效果 开始用Android Studio写了,还有挺多不明白这IDE用法的地方....蛋疼 主要思路 1. 添加了自定义的头布局    2. 默认让头布局隐藏setPadding.设置 -自身的高度  ...

  9. Android 自定义下拉刷新ListView

    package com.dwtedx.qq.view; import android.content.Context; import android.util.AttributeSet; import ...

随机推荐

  1. BZOJ 2333 左偏树 (写得我人生都崩溃了...)

    思路: 高一神犇 竟然 问我这道题   我光荣地  看着题解(划掉)  写了一下午 QaQ multiset不能erase(一个值)   这样就把等于这个值 的数都erase掉了  (woc我一开始不 ...

  2. EasyUI 编写实体类树状选择器

    <%@ page contentType="text/html;charset=UTF-8"%> <%@ include file="/WEB-INF/ ...

  3. 利用 html+css 画同心圆(concentric circles)——绝对布局与相对布局

    一.css 绘制圆 #circle { width: 300px; height: 300px; background-color: #000000; border-radius: 300px; } ...

  4. win7 64位装sql2000

    1.运行不了安装程序 右击安装exe文件->属性->兼容性->以xp sp3兼容和管理员身份 2.安装过程中提示“被挂起”的故障 解决:打开注册表编辑器,在HKEY_LOCAL_MA ...

  5. AI:模式识别的数学表示(集合—函数观点)

    前言: 模式识别的定义,参考:模式识别两种方法:知识和数据 .百科定义:模式识别(英语:Pattern Recognition),就是通过计算机用数学技术方法来研究模式的自动处理和判读.我们把环境与客 ...

  6. QT-Creator+SDK+编译器+自定义配置

    QT4.8的软件曾经耗费巨大的功夫进行构建,不舍得扔掉!重新安装Qt4.8版本 1.安装qt-creator 安装qt-creator-win-opensource-2.4.0.exe版本,不建议使用 ...

  7. KVM之virsh管理虚拟机内存

    调整内存 virsh setmem 命令帮助: [root@ubuntu ~]# virsh setmem --help NAME setmem - change memory allocation ...

  8. List分组的两种方式

    java8之前List分组 假设有个student类,有id.name.score属性,list集合中存放所有学生信息,现在要根据学生姓名进行分组. public Map<String, Lis ...

  9. eas之常用源码整理

    //查看是否有相关权限 boolean hasAllotPermission=         PermissionFactory.getRemoteInstance().hasFunctionPer ...

  10. Linux—Ubuntu14.0.5安装gitlab

    1.下载gitlab-ce,到该连接选择版本   https://mirror.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu/pool/trusty/main/g/git ...