安卓控件使用系列35:ListView列表控件显示表单的使用方法2
来源:互联网 发布:wps表格怎么刷新数据 编辑:程序博客网 时间:2024/05/22 18:35
在安卓的应用程序中经常有显示表单的功能,下面我们将这样的使用方法分享给大家。
这个例子实现的是也手机屏幕上显示三个标题对应的表单,表单中显示相应的数据列表。
整体思路:在xml文件中添加一个LinearLayout布局,在里面添加三个TextView控件,给这三个TextView控件添加文字,再添加一个ListView控件;新建一个java类MyDataSource,在这个类中定义一个getMaps方法返回一个Map类型的动态数组,里面存放三条对应TextView文字的数据,在活动中定义一个SimpleAdapter(简单适配器)类对象,用TextView的文字和对应的TextView控件ID实例化这个SimpleAdapter类对象,并把这个类对象绑定到ListView控件上。
activity_main.xml文件:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/pname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_weight="1" android:text="产品名称" android:textSize="15sp" /> <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_weight="1" android:text="产品价格" android:textSize="15sp" /> <TextView android:id="@+id/address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_weight="1" android:text="产品产地" android:textSize="15sp" /> </LinearLayout> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" ></ListView>MainActivity.java文件:
private ListView listView; private SimpleAdapter adapter; private List<Map<String, String>> data=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView=(ListView)findViewById(R.id.listview);data=MyDataSource.getMaps();adapter=new SimpleAdapter(MainActivity.this, data, R.layout.activity_main, new String[]{"pname","price","address"}, new int[]{R.id.pname,R.id.price,R.id.address});listView.setAdapter(adapter);}
MyDataSource.java文件:
public class MyDataSource {public MyDataSource() {// TODO Auto-generated constructor stub} public static List<Map<String, String>> getMaps(){List<Map<String, String>> listMaps=new ArrayList<Map<String,String>>();Map<String, String> map1=new HashMap<String, String>();map1.put("pname", "椰子");//Map相当于键值对数组map1.put("price", "¥23.30");map1.put("address", "海南");Map<String, String> map2=new HashMap<String, String>();map2.put("pname", "芒果");map2.put("price", "¥7.20");map2.put("address", "菲律宾");Map<String, String> map3=new HashMap<String, String>();map3.put("pname", "橙子");map3.put("price", "¥6.80");map3.put("address", "成都");listMaps.add(map1);listMaps.add(map2);listMaps.add(map3);return listMaps;}}
0 0
- 安卓控件使用系列35:ListView列表控件显示表单的使用方法2
- 安卓控件使用系列34:ListView列表控件显示表单的使用方法1
- 安卓控件使用系列25:Spinner下拉列表控件的使用方法
- 安卓控件使用系列33:ViewPager多页面滑动控件的使用方法2
- 安卓控件使用系列14:ImageView的基本使用方法
- 安卓ListView控件显示
- 安卓控件使用系列20:ProgressBar进度条控件的使用方法
- 安卓控件使用系列21:RatingBar评分控件的使用方法
- 安卓控件使用系列22:ScrollView垂直滚动控件的使用方法
- 安卓控件使用系列23:HorizontalScrollView 水平滚动控件的使用方法
- 安卓控件使用系列24:Gallery画廊控件的使用方法
- 安卓控件使用系列26:ImageSwitcher图片切换控件的使用方法
- 安卓控件使用系列27:GridView网格控件的使用方法
- 安卓控件使用系列31:ViewStub惰性装载控件的使用方法
- 安卓控件使用系列32:ViewPager多页面滑动控件的使用方法1
- 安卓控件使用系列29:TabHost卡片的使用方法2不继承TabActivity、TabHost控件、一个布局文件
- 列表ListView控件的使用
- ListView列表控件的使用
- Leetcode Clone Graph
- [LeetCode Problem]TwoSum
- 如何将PreparedStatement查询得到的结果集存储起来--方法一:采用对象数组
- BIOS入门之我见-开篇
- CodeVS2319 最近最远点对
- 安卓控件使用系列35:ListView列表控件显示表单的使用方法2
- win8和win10下,visual studio 2008 调试出现无响应的卡死问题解决
- 把sshkey添加到git agent上
- RPC协议、http协议、tcp/ip协议、udp协议、socket协议以及soap协议都有什么相同点和不同点
- (ZT) CHS conversion
- CCS3.3的使用2___统计程序运行的时间
- 我不经常用的
- 如何将PreparedStatement查询得到的结果集存储起来--方法二:采用列表List
- dede 搜索功能