listview动态获取数据
动态 listview 获取数据
2023-09-14 08:58:19 时间
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
1 public class MainActivity extends Activity { 3 private ListView listView; 4 private ArrayList Person persons; 5 private ListAdapter adapter; 6 private Handler handler=null; 7 //xml文件的网络地址 8 final String path="http://192.168.5.10:8080/FileServer/person.xml"; 9 @SuppressLint("HandlerLeak") 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.main); 14 listView=(ListView) super.findViewById(R.id.listview); 15 //cache=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/cache"); 17 //开一条子线程加载网络数据 18 Runnable runnable=new Runnable() 20 public void run() 22 try 24 Thread.sleep(2000); 25 //xmlwebData解析网络中xml中的数据 26 persons=XmlwebData.getData(path); 27 //发送消息,并把persons结合对象传递过去 28 handler.sendMessage(handler.obtainMessage(0, persons)); 29 } 30 catch (InterruptedException e) 32 e.printStackTrace(); 35 }; 37 try 39 //开启线程 40 new Thread(runnable).start(); 41 //handler与线程之间的通信及数据处理 42 handler=new Handler() 44 public void handleMessage(Message msg) 46 if(msg.what==0) 48 //msg.obj是获取handler发送信息传来的数据 49 @SuppressWarnings("unchecked") 50 ArrayList Person person=(ArrayList Person ) msg.obj; 51 //给ListView绑定数据 52 BinderListData(person); 55 }; 56 } 57 catch (Exception e) 59 e.printStackTrace(); 63 //绑定数据 64 public void BinderListData(ArrayList Person person) 66 //创建adapter对象 67 adapter=new ListViewAdapter(R.layout.item,this,person); 68 //将Adapter绑定到listview中 69 listView.setAdapter(adapter); 72 }
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
1 public class XmlwebData 4 private static ArrayList Person persons=null; 6 public static ArrayList Person getData(final String path) 8 try 10 URL url=new URL(path); 11 Person person=null;
13 HttpURLConnection conn=(HttpURLConnection) url.openConnection(); 14 conn.setRequestMethod("GET"); 15 conn.setConnectTimeout(5000); 16 if(conn.getResponseCode()==200) 18 InputStream inputstream=conn.getInputStream(); 21 XmlPullParser xml=Xml.newPullParser(); 22 xml.setInput(inputstream, "UTF-8"); 23 int event=xml.getEventType(); 25 while(event!=XmlPullParser.END_DOCUMENT) 27 switch (event) 29 //开始解析文档 30 case XmlPullParser.START_DOCUMENT: 31 persons=new ArrayList Person (); 32 break; 33 case XmlPullParser.START_TAG: 35 String value=xml.getName(); 36 if(value.equals("person")) 37 {//person对象的初始化必须在这里初始化不然可能出现为null的现象 38 person=new Person(); 39 //获取属性值 40 person.setId(new Integer(xml.getAttributeValue(0))); 42 else if(value.equals("name")) 44 person.setName(xml.nextText()); 46 else if(value.equals("sex")) 48 person.setSex(xml.nextText()); 50 else if(value.equals("age")) 52 person.setAge(new Integer(xml.nextText())); 54 else if(value.equals("path")) 56 person.setPath(xml.nextText()); 58 break; 59 case XmlPullParser.END_TAG: 60 if(xml.getName().equals("person")) 62 persons.add(person); 63 System.out.println(person.getName());; 64 person=null; 66 break; 68 //解析下一个对象 69 event=xml.next(); 71 return persons; 73 } 74 catch (Exception e) 76 e.printStackTrace(); 77 } 80 return null; 84 }
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
1 public class Person 3 private int id; 4 private String name; 5 private String sex; 6 private String path; 7 public String getPath() { 8 return path; 10 public void setPath(String path) { 11 this.path = path; 13 private int age; 14 public int getId() { 15 return id; 17 public void setId(int id) { 18 this.id = id; 20 public String getName() { 21 return name; 23 public void setName(String name) { 24 this.name = name; 26 public String getSex() { 27 return sex; 29 public void setSex(String sex) { 30 this.sex = sex; 32 public int getAge() { 33 return age; 35 public void setAge(int age) { 36 this.age = age; 38 public Person(){ 41 }
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
1 public class ListViewAdapter extends BaseAdapter implements ListAdapter 4 private ArrayList Person data; 5 private int id; 6 private Context context; 7 private LayoutInflater inflater; 8 public ListViewAdapter(int item, MainActivity mainActivity,ArrayList Person data) 10 this.data=data; 11 this.context=mainActivity; 12 this.id=item; 13 inflater=LayoutInflater.from(context); 16 @Override 17 public int getCount() 19 return data.size(); 22 @Override 23 public Object getItem(int position) 25 return data.get(position); 28 @Override 29 public long getItemId(int position) 31 return position; 34 @Override 35 public View getView(int position, View view, ViewGroup arg2) 37 TextView name=null; 38 TextView sex=null; 39 TextView age=null; 40 ImageView img=null; 41 if(view==null) 43 view=inflater.inflate(id, null); 44 name=(TextView) view.findViewById(R.id.PersonName); 45 sex=(TextView) view.findViewById(R.id.PersonSex); 46 age=(TextView) view.findViewById(R.id.PersonAge); 47 img=(ImageView) view.findViewById(R.id.Personimage); 48 //保存view对象到ObjectClass类中 49 view.setTag(new ObjectClass(name,sex,age,img)); 51 else 53 //得到保存的对象 54 ObjectClass object "性别:"+person.getSex().toString()); 65 age.setText("年龄:"+String.valueOf(person.getAge())); 66 //加载图片资源 67 LoadImage(img,person.getPath()); 68 return view; 72 private void LoadImage(ImageView img, String path) 74 //异步加载图片资源 75 AsyncTaskImageLoad async=new AsyncTaskImageLoad(img); 76 //执行异步加载,并把图片的路径传送过去 77 async.execute(path); 81 private final class ObjectClass 83 TextView name=null; 84 TextView sex=null; 85 TextView age=null; 86 ImageView img=null; 87 public ObjectClass(TextView name, TextView sex, TextView age,ImageView img) 89 this.name=name; 90 this.sex=sex; 91 this.age=age; 92 this.img=img; 97 }
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
1 public class AsyncTaskImageLoad extends AsyncTask String, Integer, Bitmap { 3 private ImageView Image=null; 5 public AsyncTaskImageLoad(ImageView img) 7 Image=img; 9 //运行在子线程中 10 protected Bitmap doInBackground(String... params) { 11 try 13 URL url=new URL(params[0]); 14 HttpURLConnection conn=(HttpURLConnection) url.openConnection(); 15 conn.setRequestMethod("POST"); 16 conn.setConnectTimeout(5000); 17 if(conn.getResponseCode()==200) 19 InputStream input=conn.getInputStream(); 20 Bitmap map=BitmapFactory.decodeStream(input); 21 return map; 23 } catch (Exception e) 25 e.printStackTrace(); 27 return null; 30 protected void onPostExecute(Bitmap result) 32 if(Image!=null result!=null) 34 Image.setImageBitmap(result); 37 super.onPostExecute(result); 39 }
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
1 ?xml version="1.0" encoding="UTF-8"? 2 Persons 3 person id="1" 4 name 张三 /name 5 sex 男 /sex 6 age 25 /age 7 path http://192.168.5.10:8080/FileServer/chengjisihan.jpg /path 8 /person 9 person id="2" 10 name 李斯 /name 11 sex 男 /sex 12 age 78 /age 13 path http://192.168.5.10:8080/FileServer/laozi.jpg /path 14 /person 15 person id="3" 16 name 王五 /name 17 sex 男 /sex 18 age 22 /age 19 path http://192.168.5.10:8080/FileServer/lilongji.jpg /path 20 /person 21 person id="4" 22 name 庞聪 /name 23 sex 男 /sex 24 age 31 /age 25 path http://192.168.5.10:8080/FileServer/lishimin.jpg /path 26 /person 27 person id="5" 28 name 孙膑 /name 29 sex 男 /sex 30 age 48 /age 31 path http://192.168.5.10:8080/FileServer/lisi.jpg /path 32 /person 33 person id="6" 34 name 孙武 /name 35 sex 男 /sex 36 age 58 /age 37 path http://192.168.5.10:8080/FileServer/liyuan.jpg /path 38 /person 40 person id="7" 41 name 成吉思汗 /name 42 sex 男 /sex 43 age 40 /age 44 path http://192.168.5.10:8080/FileServer/sunbiin.jpg /path 45 /person 47 person id="8" 48 name 李渊 /name 49 sex 男 /sex 50 age 36 /age 51 path http://192.168.5.10:8080/FileServer/sunwu.jpg /path 52 /person 54 person id="9" 55 name 李隆基 /name 56 sex 男 /sex 57 age 32 /age 58 path http://192.168.5.10:8080/FileServer/wangwu.jpg /path 59 /person 60 person id="10" 61 name 武则天 /name 62 sex 女 /sex 63 age 55 /age 64 path http://192.168.5.10:8080/FileServer/wuzetian.jpg /path 65 /person 66 /Persons
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![](http://images.cnitblog.com/blog/536313/201307/18172957-568c42b7d3294a6385e4d3f58a8780ca.jpg)
更高效地刷新 RecyclerView | DiffUtil二次封装 每次数据变化都全量刷新整个列表是很奢侈的,不仅整个列表会闪烁一下,而且所有可见表项都会重新绑定一遍数据。这一篇对 DiffUtil 进行二次封装以让其更易于使用。
更好的 RecyclerView 表项子控件点击监听器 上篇介绍了一种新的监听 RecyclerView 表项点击事件的方法。实现了将点击事件和RecyclerView.Adapter解耦。这一篇介绍如何监听 RecyclerView 表项子控件点击事件。
相关文章
- 如何解决何避免多个C/C++动态库函数同名冲突
- 一颗红心,三手准备,分别基于图片(img)/SCSS(样式)/SVG动画实现动态拉轰的点赞按钮特效
- K8s pod 动态弹性扩缩容(HPA )部署!步骤齐全,少走坑路
- 动态规划模型:背包二维费用问题
- R语言中贝叶斯网络(BN)、动态贝叶斯网络、线性模型分析错颌畸形数据|附代码数据
- 【Android NDK 开发】Android Studio 使用 CMake 导入动态库 ( 构建脚本路径配置 | 指定动态库查找路径 | 链接动态库 )
- 【Android 应用开发】动态权限管理示例 ( 使用原生代码实现 | 申请权限 | 判定权限申请结果 | 判定 “ 不再询问 “ 情况 )
- 浅谈Java动态代理详解编程语言
- MySQL动态插入变量实现实时更新(mysql插入变量)
- 自开发程序动态权限设置按钮详解编程语言
- 利用PHP动态生成VRML网页
- 使用ajax技术无刷新动态调用股票信息
- ScrollView与ListView合用(正确计算Listview的高度)的问题解决
- 动态加载脚本提升javascript性能
- jQuery获取动态生成的元素示例