组件的使用(二)ListView的使用
来源:互联网 发布:超声波洗菜机 知乎 编辑:程序博客网 时间:2024/06/05 10:49
ListView的使用中,可选择继承ListActivity,也可选择不继承。
继承ListActivity的实例:
http://blog.csdn.net/jincf2011/article/details/6605867
http://xys289187120.blog.51cto.com/3361352/657171/
不继承ListActivity的实例:
分三步:
1.在activity_main.xml文件中声明listview
<ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:drawSelectorOnTop="false" android:background="#fff" />
2.自定义item样式
在layout目录下新建list_tem.xml文件
内容可参考:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="41dp" android:orientation="horizontal" android:background="#fff"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_weight="1" > <TextView android:id="@+id/list_item_textview_name" android:layout_width="fill_parent" android:layout_height="40dp" android:text="蒋XX" android:gravity="center" /> <!-- 一条横线--> <View android:layout_width="match_parent" android:layout_height="1dip" android:background="#cccccc" android:layout_gravity="center_horizontal" /> </LinearLayout> <!-- 竖线--> <View android:layout_width="1dip" android:layout_height="match_parent" android:background="#cccccc" android:layout_gravity="center_horizontal" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_weight="1" > <TextView android:id="@+id/list_item_textview_state" android:layout_width="fill_parent" android:layout_height="40dp" android:text="正在设计中" android:gravity="center" /> <!-- 一条横线--> <View android:layout_width="match_parent" android:layout_height="1dip" android:background="#cccccc" android:layout_gravity="center_horizontal" /> </LinearLayout> <!-- 一条竖线--> <View android:layout_width="1dip" android:layout_height="match_parent" android:background="#cccccc" android:layout_gravity="center_horizontal" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_weight="0.8" > <TextView android:id="@+id/list_item_textview_date" android:layout_width="fill_parent" android:layout_height="40dp" android:text="2015/6/27" android:gravity="center" /> <!-- 一条横线--> <View android:layout_width="match_parent" android:layout_height="1dip" android:background="#cccccc" android:layout_gravity="center_horizontal" /> </LinearLayout> </LinearLayout>
视图:
3.在代码中实现listview
此处只提供一个处理ListView的方法,以及需要的数据。
//数据域 private SimpleAdapter listItemAdapter;//SimpleAdapter作为ListView的适配器 private ListView mListView;//ListView private ArrayList<HashMap<String, Object>> listItems;//存放ListView Items资源 //此方法处理ListView private void processListView(){ mListView=(ListView)findViewById(R.id.listview); listItems = new ArrayList<HashMap<String, Object>>(); for(int i=0;i<12;i++){ HashMap<String, Object> map = new HashMap<String, Object>(); map.put("ItemName", "name: "+i); map.put("ItemState", "正在进行中"); map.put("ItemDate", "2015.5."+i); listItems.add(map); } listItemAdapter=new SimpleAdapter(this, listItems, R.layout.list_item, new String[]{"ItemName","ItemState","ItemDate"},new int[]{R.id.list_item_textview_name,R.id.list_item_textview_state,R.id.list_item_textview_date}); mListView.setDividerHeight(0);//设置边界线不可见 mListView.setAdapter(listItemAdapter); mListView.setOnItemClickListener(new OnItemClickListener() {//为item设置点击事件 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 获取点击的Item视图中一个textview的值 TextView textView=(TextView)view.findViewById(R.id.list_item_textview_name); String strName=textView.getText().toString().trim(); } }); }
1 0
- 组件的使用(二)ListView的使用
- ListView组件的使用
- ListView组件的使用
- ListView组件的使用
- ListView组件的使用
- ListView的使用(二)
- ListView的使用(二)
- android ListView的使用 (二)
- 自定义使用Adapter的组件(二)
- Adnroid组件的使用:ListView与GridView
- 【android】5.listview组件的使用(重要!)
- TabHost组件和ListView组件的结合使用
- ListView 中使用第三方组件 二
- Android学习—ListView的使用(二)
- Angular基础(二) 组件的使用
- android 常用控件listview的使用(二)
- Android ListView/GridView 中Adapter的使用(二)
- 流程图控件FlowChart.NET组件的使用(二)
- 单行横向滑动的日历calendar控件
- 05 魔方
- iOS开发-Day25-UIView
- 更新日志 - BugHD Android 客户端上线
- linux(六)
- 组件的使用(二)ListView的使用
- Opencv::findContours函数参数说明及相关函数
- leetcode_171 Excel Sheet Column Number & leetcode_28 Implement strStr()
- 动态规划之子序列与子串问题分析
- 定时器的简单实现即回调函数的运用
- minHash(最小哈希)和LSH(局部敏感哈希)
- 利用keepalived实现双台服务器主备高可用
- HDOJ 2544 最短路(最短路径 dijkstra算法,SPFA邻接表实现,floyd算法)
- Ansible