博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ANDROID_MARS学习笔记_S01原始版_006_ListView
阅读量:4991 次
发布时间:2019-06-12

本文共 2947 字,大约阅读时间需要 9 分钟。

一、代码

1.xml
(1)main.xml

1 
2
5
8
11
12

 

(2)user.xml

 

1 
2
7
10
13

 

 

 

2.java

(1)Activity01.java

1 package mars.listview; 2  3 import java.util.ArrayList; 4 import java.util.HashMap; 5  6 import android.app.ListActivity; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.widget.ListView;10 import android.widget.SimpleAdapter;11 12 public class Activity01 extends ListActivity {13     /** Called when the activity is first created. */14     @Override15     public void onCreate(Bundle savedInstanceState) {16         super.onCreate(savedInstanceState);17         setContentView(R.layout.main);18         ArrayList
> list = new ArrayList
>();19 HashMap
map1 = new HashMap
();20 HashMap
map2 = new HashMap
();21 HashMap
map3 = new HashMap
();22 map1.put("user_name", "zhangsan");23 map1.put("user_ip", "192.168.0.1");24 map2.put("user_name", "zhangsan");25 map2.put("user_ip", "192.168.0.2");26 map3.put("user_name", "wangwu");27 map3.put("user_ip", "192.168.0.3");28 list.add(map1);29 list.add(map2);30 list.add(map3);31 MyAdapter listAdapter = new MyAdapter(this, list,32 R.layout.user, new String[] { "user_name", "user_ip" },33 new int[] { R.id.user_name,R.id.user_ip});34 setListAdapter(listAdapter);35 }36 37 @Override38 protected void onListItemClick(ListView l, View v, int position, long id) {39 // TODO Auto-generated method stub40 super.onListItemClick(l, v, position, id);41 System.out.println("id----------------" + id);42 System.out.println("position----------" + position);43 }44 45 }

 

(2)MyAdapter.java

 

1 package mars.listview; 2  3 import java.util.List; 4 import java.util.Map; 5  6 import android.R.color; 7 import android.content.Context; 8 import android.view.LayoutInflater; 9 import android.view.View;10 import android.view.ViewGroup;11 import android.widget.SimpleAdapter;12 13 public class MyAdapter extends SimpleAdapter {14     private LayoutInflater inflater = null;15     private List
> styles = null;16 public List
> getStyles() {17 return styles;18 }19 20 public void setStyles(List
> styles) {21 this.styles = styles;22 }23 24 @Override25 public View getView(int position, View convertView, ViewGroup parent) {26 // TODO Auto-generated method stub27 View result = super.getView(position, convertView, parent);28 System.out.println("positon---->" + position);29 if(result != null){30 inflater.inflate(R.layout.user1, null);31 }32 return result;33 }34 35 public MyAdapter(Context context, List
> data,36 int resource, String[] from, int[] to) {37 super(context, data, resource, from, to);38 inflater = LayoutInflater.from(context);39 }40 41 }

 

转载于:https://www.cnblogs.com/shamgod/p/5188069.html

你可能感兴趣的文章
[优化]JavaScript 格式化带有占位符字符串
查看>>
打JAR包
查看>>
大图轮播
查看>>
UNIX环境高级编程读书笔记
查看>>
java awt 乱码问题
查看>>
矩阵中的路径
查看>>
unity回调函数范例
查看>>
linux下给php安装curl、gd(ubuntu)
查看>>
Java自带的Logger使用-代码摘要
查看>>
Java设计模式系列 — 构造器模式
查看>>
MySQL执行计划explain的key_len解析
查看>>
Windows Phone开发(9):关于页面状态 转:http://blog.csdn.net/tcjiaan/article/details/7292160...
查看>>
android 通过数组,流播放声音的方法
查看>>
Spring入门篇
查看>>
JAVA遇见HTML——JSP篇(JSP状态管理)
查看>>
启动eclipse出现错误Java was started but returned exit =一个数字
查看>>
myBatis模糊查找
查看>>
数据结构与算法之五 链接列表
查看>>
java 对象数组
查看>>
设计模式读书笔记-单件模式(创建型模式)
查看>>