码迷,mamicode.com
首页 > 其他好文 > 详细

第十九讲:ListView与Adapter(二)

时间:2014-11-02 09:25:27      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   color   os   ar   使用   

会当凌绝顶,一览众山小。 —— 杜  甫《望岳》


本讲内容:ListView列表组件 与 Adapter适配器的用法


一、ListView使用SimpleAdapter

很多时候需要在列表中展示一些除了文字以外的东西,比如图片等。这时候可以使用SimpleAdapter可以通过它

使用simpleAdapter的数据一般都是用HashMap构成的列表,列表的每一节对应ListView的每一行。通过SimpleAdapter的构造函数,将HashMap的每个键的数据映射到布局文件中对应控件上。这个布局文件一般根据自己的需要来自定义ListView中的item的内容,比如图片、多选框等。


二、使用SimpleAdapter的步骤。

1)根据需要定义ListView每行所实现的布局。

2)定义一个HashMap构成的列表,将数据以键值对的方式存放在里面。

3)构造SimpleAdapter对象。

4)将LsitView绑定到SimpleAdapter上。


我们通过下面例子感受一下,代码的讲解都写在注释里了

下面是res/layout/activity_main.xml 布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.text.MainActivity$PlaceholderFragment" >
 
 <ListView 
     android:id="@+id/listViewId"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>
    
</LinearLayout>

新建一个res/layout/item.xml 布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <ImageView 
        android:id="@+id/ItemImageId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"/>
    
    <TextView 
        android:id="@+id/ItemTitleId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#ff00ff"/>
    <TextView 
        android:id="@+id/ItemTextId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#ffff00"
        android:layout_below="@+id/ItemTitleId"/>

</RelativeLayout>

下面是MainActivity.java主界面文件:

public class MainActivity extends Activity {
	private ListView listView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		listView = (ListView) findViewById(R.id.listViewId);

		// 定义一个动态数组
		ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
		// 在数组中存放数据
		for (int i = 0; i < 10; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ItemImage", R.drawable.ic_launcher);
			map.put("ItemTitle", "第" + i + "行");
			map.put("ItemText", "这是第" + i + "行");
			listItem.add(map);
		}

		SimpleAdapter adapter = new SimpleAdapter(this, listItem,R.layout.item, new String[] { "ItemImage", 
	    "ItemTitle","ItemText" }, new int[] { R.id.ItemImageId,R.id.ItemTitleId, R.id.ItemTextId});
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?>parent, View view,int position, long id) {
				//点击后在标题上显示点击了第几行                  
				setTitle("你点击了第"+position+"行");
			}
		});
	}

SimpleAdapter<CharSequence>,具有五个参数
第一个是conetxt,也就是application的环境,可以设置为this,也可以通过getContext()获取. 
 第二个参数表示生成一个Map(String ,Object)列表选项
 第三个参数表示界面布局的id  表示该文件作为列表项的组件
 第四个参数表示该Map对象的哪些key对应value来生成列表项
 第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系
 注意的是map对象可以key可以找不到 但组件的必须要有资源填充  因为 找不到key也会返回null 其实就相当于给了一个null资源

下面是运行结果:
bubuko.com,布布扣

本讲到这里,谢谢大家!


第十九讲:ListView与Adapter(二)

标签:android   style   blog   http   io   color   os   ar   使用   

原文地址:http://blog.csdn.net/liguojin1230/article/details/40685115

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!