您的位置:首页 > 其它

ListView(SimpleAdapter)

2012-01-05 11:02 357 查看
www.csdn.net

看名字大家都知道什么意思,所以直接先看个实例,再讲。可能和以往接触的用法有点区别,但是还是一个道理。

listview1.xml完整源代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listview1listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>


listitem1.xml完整源代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="@+id/MyListItem"
android:paddingBottom="3dip"
android:paddingLeft="10dip" >
<TextView
android:id="@+id/listitem1ItemTitle"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="30dip"
/>
<TextView
android:id="@+id/listitem1ItemText"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
</LinearLayout>


ListViewActivity.java完整源代码
package my.android.activity;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ListViewActivity1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview1);
ListView listView = (ListView) findViewById(R.id.listview1listview);
//这应该向数据库请求数据
ArrayList<HashMap<String, String>> mylist =
new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 30; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("ItemTitle", "This is Title"+(i+1));
map.put("ItemText", "This is text"+(i+1));
mylist.add(map);
}
// 生成适配器,数组===》ListItem
SimpleAdapter adapter = new SimpleAdapter(
this,
mylist,
R.layout.listitem1,
new String[] { "ItemTitle", "ItemText" },
new int[] { R.id.listitem1ItemTitle, R.id.listitem1ItemText });
listView.setAdapter(adapter);
}
}


其实重点是SimpleAdapter,对于它的这5个参数的构造方法,我当时是一头雾水。看官方文档还是没能搞明白,怎么办?对于这种UI就多尝试,把你能想到的东西往上试就好了。下面我们就慢慢试验。

首先看一下SimpleAdapter的第一参数Contentcontext,我们传得是this,说实在的对于这个我不理解,为什么这儿要由这个参数?

第二个参数List<?extends Map<String,?>> data,应该能够理解了,就是它的数据来源。

第三个参数int resource,是一个int值,再看看我们的代码,有两个xml,我起的名字叫做listview1.xml,listitem1.xml一看就知道了。ListView控件的每一行(item)里面显示的内容是什么?这个listitem1.xml里面的内容是什么,那么每一行显示出来的就是什么!

现在我们来对listitem1.xml做下修改,其他的代码不做任何的改动。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="@+id/MyListItem"
android:paddingBottom="3dip"
android:paddingLeft="10dip" >
<ImageView
android:id="@+id/listitem1imageview"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:src="@drawable/ic_launcher"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/listitem1ItemTitle"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="30dip"
/>
<TextView
android:id="@+id/listitem1ItemText"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
</LinearLayout>
</LinearLayout>


显示的内容是这样的,如下面的左图



你还可以在第二层的LinearLayout里面的两个TextView下面再加上个EditText试试,如上面的右图。

好了,第三个参数理解了吧。

再来看看第四个参数和第五个参数,一起来看。我们把ArrayList和SimpleAdapter的对象拿出来看看,之间的异同。
ArrayList<HashMap<String, String>> mylist = new
ArrayList<HashMap<String, String>>();
for (int i = 0; i < 30; i++) {
HashMap<String, String> map =
new HashMap<String, String>();
map.put("ItemTitle", "This is Title"+(i+1));
map.put("ItemText", "This is text"+(i+1));
mylist.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(
this,
mylist,
R.layout.listitem1,
new String[] { "ItemText", "ItemTitle" },
new int[] { R.id.listitem1ItemTitle,
R.id.listitem1ItemText });


发现了没map.put的key是ItemTitle,ItemText;第四个参数也是ItemTitle和ItemText。我们如果换个位置会怎么样呢?好把第四个参数

new String[] { "ItemText", "ItemTitle" }改成

new String[] { "ItemTitle", "ItemText" }

结果显示的如下面的第一幅图。发现只是两个文本的内容交换了一下。

好,我们再改一下(改之前,把上面的这第四个参数改回去),

new String[] { "ItemTitle", "ItemText" }改成

new String[] { "ItemTitleTitle", "ItemTextsss" }

结果显示的如下面的第二幅图。发现没有了title。



再把代码改回去,再做改动

new int[] { R.id.listitem1ItemTitle,
R.id.listitem1ItemText });改成
new int[] { R.id. listitem1ItemText,
R.id. listitem1ItemTitle});
结果显示的如上面的第三幅图。发现两个文本内容换位置了。

好,是不是向着我们希望的方向发展呢?在layout下新建一个aa.xml,里面只是定义一个TextView,id为aatextview。

new int[] { R.id.listitem1ItemTitle,
R.id.listitem1ItemText });改成

new int[] { R.id. aatextview,
R.id. listitem1ItemTitle});
结果如上面的第四幅图。第一个文本的内容为空。

好了,上面试了那么多,下面来对着官方文档重新理解一下。

publicSimpleAdapter (Context context, List<? extends Map<String, ?>> data,

intresource, String[] from, int[] to)

Constructor

Parameters

context
The context where the View associated with this SimpleAdapter is running

data
A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"

resource
Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"

from
A list of column names that will be added to the Map associated with each item.

to
The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

第一个参数context,上下文的意思,翻译过来是这样的:SimpleAdapter所要运行关联到的视图,这个是什么呢?就是你这个SimpleAdapter所在的Activity(一般而言),所以这个参数一般是this。但是这儿为什么需要这个参数,真心不明白。。。现在没那个能力去研究源代码,记下来,以后搞。

第二个参数data,翻译过来是这样的。装着Map的List对象,List中的每个实体都关联到ListView中的每一行(在这儿我就这样翻译了)。List中的Map对象包含了每一行中的数据,并且应该和from参数中的相对应。

翻译的不好,实际上就是这个意思,List中是放Map的,而每一个Map对应到ListView中的每一行,并且Map中的put的key要和from参数中的相对应。我们上面改动from的时候,结果就会变化,同理我们改动map中的是一个道理,这个好理解了吧。

第三个参数resource,翻译过来是这样的。它的值应该是一个layout文件夹下的某个xml的id引用,这个xml里面定义了我们ListView中的listitem需要的控件,xml中应该至少包括to参数中定义的名字。

看看上面我们多加了个ImageView?可以正常显示的。

再看看我们把to参数中的值改成别的xml中的id,就不是我们想要的结果了。

第四个参数from,意思就是,from里面的值和Map里面的key应该对应起来。

第五个参数to,就是我们listitem1.xml文件中定义的控件显示的内容取决于from参数的值。这些控件应该全部是TextView控件。而且to参数的第N个列(也就是数组的第N个值)关联到的控件所显示的内容取决于from参数的第N个列。

OK ,终于结束了。最主要讲的就是SimpleAdapter这个类的构造方法和ListView的结合,但是SimpleAdapter还可以和其他控件一起用,不是单独为ListView设计的。

虽然上面是Simple,但是实际上对于我们这样的新手还是有点不易理解的。花了一个上午学习ListView的基础和写这篇心得。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: