ListActivity。这个类其实就是一个含有一个ListView组件的Activity类。
也就是说,如果我们直接在一个普通的Activity中自己加一个ListView也是完全可以取代这个ListActivity的,只是它更方便而已。
packagecom.teleca;
importjava.util.List;
importjava.util.ArrayList;
importandroid.app.ListActivity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.util.Log;
importandroid.widget.ArrayAdapter;
importandroid.widget.TextView;
importandroid.widget.ListView;
publicclass HelloListextendsListActivity{
publicvoidonCreate(Bundleicicle){
super.onCreate(icicle);
setContentView(R.layout.main);
List<String>items=fillArray();
ArrayAdapter<String>adapter=newArrayAdapter<String>(this,R.layout.list_row,items);
this.setListAdapter(adapter);
}
privateList<String>fillArray(){
List<String>items=newArrayList<String>();
items.add("1");
items.add("2");
items.add("3");
items.add("4");
items.add("5");
items.add("6");
items.add("7");
returnitems;
}
@Override
protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid)
{
TextViewtxt=(TextView)this.findViewById(R.id.text);
txt.setText("selected"+l.getSelectedItem().toString()+"!");
}
}
注意:R.layout.list_row里面应该有个TextView
public ArrayAdapter(Contextcontext,inttextViewResourceId,List<T>objects)
Since:APILevel1
Constructor
Parameters
contextThecurrentcontext.
textViewResourceIdTheresourceIDforalayoutfilecontainingaTextViewtousewheninstantiatingviews.
objectsTheobjectstorepresentintheListView.
public ArrayAdapter(Contextcontext,intresource,inttextViewResourceId,List<T>objects)
Since:APILevel1
Constructor
Parameters
contextThecurrentcontext.
resourceTheresourceIDforalayoutfilecontainingalayouttousewheninstantiatingviews.
textViewResourceIdTheidoftheTextViewwithinthelayoutresourcetobepopulated
objectsTheobjectstorepresentintheListView.
的确可以简单到只需准备一个List对象并借助Adapter就可以构造出一个列表。
重载 onListItemClick() 方法可以响应选择事件,利用第一个参数可以访问到这个ListView实例以得到选中的条目信息。
这里有一点要说明的,就是如果更简单的话,其实连那个setContentView都可以不要了,Android也会自动帮我们构造出一个全屏的列表。但是本例中我们需要一个TextView来显示选中的条目,所以我们需要一个layout.main描述一下这个列表窗口。
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextViewid="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<ListView id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
<wbr style="line-height:25px">这里需要注意的是那个ListView的ID,是系统自定义的android:list,不是我们随便取的,否则系统会说找不到它想要的listview了。</wbr><wbr style="line-height:25px"><br style="line-height:25px"> 然后,在这个listview之外,我们又增加了一个TextView,用来显示选中的条目。<br style="line-height:25px"><span style="color:#000080; line-height:25px"><wbr style="line-height:25px">再来说说这里用到的ArrayAdapter,它的构造函数中第二个参数是一个布局资源ID,如果文件只有一个控件的话,那么就不用指定控件的id,否则需要指定控件的id,<br style="line-height:25px"> ArrayAdapter的API文档中说是要求用一个包含TextView的layout文件,平台用它来控制列表项目的显示,</wbr></span><br style="line-height:25px"> 这里的取值是<span style="color:#993300; line-height:25px">R.layout.list_row</span>,所以,我们还有一个list_row.xml文件来描述这个布局,相当简单。<br style="line-height:25px"><span style="line-height:25px">list_row.xml文件</span>:<br style="line-height:25px"> <?xmlversion="1.0"encoding="utf-8"?><br style="line-height:25px"> <TextViewandroid:id="@+id/item"<br style="line-height:25px"> xmlns:android="http://schemas.android.com/apk/res/android"<br style="line-height:25px"> android:layout_width="wrap_content"<br style="line-height:25px"> android:layout_height="wrap_content"/><br style="line-height:25px"> 注:<br style="line-height:25px"> 去掉项与项的横线<br style="line-height:25px"> listView.setDividerHeight(0);<br style="line-height:25px"> this.getListView().setDivider(null);<br style="line-height:25px"> 这两个方法应该都可以,配合使用。<br style="line-height:25px"><span style="color:#003366; line-height:25px">从ArrayAdapter上溯到BaseAdapter,发现还有几个同源的Adapter也应该可以使用,象SimpleAdapter和CursorAdapter。<br style="line-height:25px"> 先看看SimpleAdapter,说是simple却不简单哦。</span> <div style="line-height:25px"> <span style="color:#003366; line-height:25px"><span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span></span> <h4 style="line-height:25px; margin-top:1.25em; margin-right:0px; margin-bottom:0.65em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:1.1em; background-color:rgb(226,226,226); color:rgb(58,58,58)"> <span style="line-height:23px; font-size:0.9em">public</span><span style="line-height:25px; margin-right:2px">SimpleAdapter</span><span style="line-height:23px; font-size:0.9em">(<a rel="nofollow" href="http://developer.android.com/reference/android/content/Context.html" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">Context</a>context,<a rel="nofollow" href="http://developer.android.com/reference/java/util/List.html" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">List</a><?extends<a rel="nofollow" href="http://developer.android.com/reference/java/util/Map.html" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">Map</a><<a rel="nofollow" href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">String</a>,?>> data, int resource,<a rel="nofollow" href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">String[]</a>from, int[] to)</span> </h4> <div style="line-height:18px; margin-top:-25px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:7px; padding-bottom:0px; padding-left:7px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:0.8em; color:rgb(153,153,153); float:right"> <div style="line-height:18px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> Since:<a rel="nofollow" href="http://developer.android.com/guide/appendix/api-levels.html#level1" style="color:rgb(153,153,153); line-height:18px; text-decoration:none">API Level 1</a> </div> </div> <div style="line-height:23px; margin-top:0.5em; margin-right:0.25em; margin-bottom:0.5em; margin-left:0.25em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <div style="line-height:1em; margin-top:0.25em; margin-right:0px; margin-bottom:0.75em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <p style="line-height:1.3em; margin-top:0.5em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> Constructor</p> </div> <div style="line-height:23px; margin-top:0.5em; margin-right:1em; margin-bottom:0.5em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <h5 style="line-height:23px; margin-top:1em; margin-right:0px; margin-bottom:0.65em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:1em; color:rgb(58,58,58)"> Parameters</h5> <table style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:1em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:1em; border-collapse:collapse; empty-cells:show"><tbody style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <tr style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial; font-style:italic"> context</th> <td style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial"> The context where the View associated with this SimpleAdapter is running</td> </tr> <tr style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial; font-style:italic"> data</th> <td style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial"> 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"</td> </tr> <tr style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial; font-style:italic"> resource</th> <td style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial"> 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"</td> </tr> <tr style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial; font-style:italic"> from</th> <td style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial"> A list of column names that will be added to the Map associated with each item.</td> </tr> <tr style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial; font-style:italic"> to</th> <td style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial"> 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.</td> </tr> </tbody></table> </div> </div> 首先看看这个fillMaps方法,基本上就明白这个simpleAdapter是怎么回事了,在有些场合它还是挺有用的,可以为每个条目绑定一个值:<br style="line-height:25px"> privateList<HashMap<String,String>>fillMaps()<br style="line-height:25px"> ...{<br style="line-height:25px"> List<HashMap<String,String>>items=newArrayList<HashMap<String,String>>();<br style="line-height:25px"><br style="line-height:25px"> HashMap<String,String>i=newHashMap<String,String>();<br style="line-height:25px"> i.put("name","日曜日");<br style="line-height:25px"> i.put("key","SUN");<br style="line-height:25px"> items.add(i);<br style="line-height:25px"> HashMap<String,String>i1=newHashMap<String,String>();<br style="line-height:25px"> i1.put("name","月曜日");<br style="line-height:25px"> i1.put("key","MON");<br style="line-height:25px"> items.add(i1);<br style="line-height:25px"> HashMap<String,String>i2=newHashMap<String,String>();<br style="line-height:25px"> i2.put("name","火曜日");<br style="line-height:25px"> i2.put("key","TUE");<br style="line-height:25px"> items.add(i2);<br style="line-height:25px"> HashMap<String,String>i3=newHashMap<String,String>();<br style="line-height:25px"> i3.put("name","水曜日");<br style="line-height:25px"> i3.put("key","WED");<br style="line-height:25px"> items.add(i3);<br style="line-height:25px"> HashMap<String,String>i4=newHashMap<String,String>();<br style="line-height:25px"> i4.put("name","木曜日");<br style="line-height:25px"> i4.put("key","THU");<br style="line-height:25px"> items.add(i4);<br style="line-height:25px"> HashMap<String,String>i5=newHashMap<String,String>();<br style="line-height:25px"> i5.put("name","金曜日");<br style="line-height:25px"> i5.put("key","FRI");<br style="line-height:25px"> items.add(i5);<br style="line-height:25px"> HashMap<String,String>i6=newHashMap<String,String>();<br style="line-height:25px"> i6.put("name","土曜日");<br style="line-height:25px"> i.put("key","SAT");<br style="line-height:25px"> items.add(i6);<br style="line-height:25px"><br style="line-height:25px"> returnitems;<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> 然后,在HelloTwoB中的onCreate函数中,修改代码,有几个不同:items的元素是HashMap实例,这是一点变化,然后构造函数除了要求items以外,还要求提供一个string[]来说明用hash表中的哪个字段显示在列表中,而后是一个控件ID的数组。我的代码是这样的:<br style="line-height:25px"><span style="color:#808080; line-height:25px">//SimpleAdapterdemo</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">List<HashMap<String,String>>items=fillMaps();</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">SimpleAdapteradapter=newSimpleAdapter(this,items,R.layout.list_row,newString[]...{"name"},newint[]...{R.id.item});</span><br style="line-height:25px"><br style="line-height:25px"> 编译跑一下可以看到结果了,是吧?只是显示的文字不太对,再改一下:<br style="line-height:25px"><br style="line-height:25px"> protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid)<br style="line-height:25px"> ...{<br style="line-height:25px"> TextViewtxt=(TextView)this.findViewById(R.id.text);<br style="line-height:25px"> txt.setText("あすは"+((HashMap)l.obtainItem(position)).get("key").toString()+"です。");<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> 这样就好多了,其实一般情况下我们都是用ListView中的obtainItem取得当前选中的条目,然后转成List中的对应类型来使用的。<br style="line-height:25px"><br style="line-height:25px"> 上面的例子中只显示name对应的值,其实你也可以试一下这样:<br style="line-height:25px"><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">SimpleAdapteradapter=newSimpleAdapter(this,items,R.layout.list_row,newString[]...{"name","key"},newint[]...{R.id.item,R.id.item2});</span><br style="line-height:25px"> 看看是什么效果。</div> <div style="line-height:25px"> <span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <h4 style="line-height:25px; margin-top:1.25em; margin-right:0px; margin-bottom:0.65em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:1.1em; background-color:rgb(226,226,226); color:rgb(58,58,58)"> <span style="line-height:23px; font-size:0.9em">public</span><span style="line-height:25px; margin-right:2px">SimpleCursorAdapter</span><span style="line-height:23px; font-size:0.9em">(<a rel="nofollow" href="http://developer.android.com/reference/android/content/Context.html" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">Context</a>context, int layout,<a rel="nofollow" href="http://developer.android.com/reference/android/database/Cursor.html" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">Cursor</a>c,<a rel="nofollow" href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">String[]</a>from, int[] to)</span> </h4> <div style="line-height:18px; margin-top:-25px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:7px; padding-bottom:0px; padding-left:7px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:0.8em; color:rgb(153,153,153); float:right"> <div style="line-height:18px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> Since:<a rel="nofollow" href="http://developer.android.com/guide/appendix/api-levels.html#level1" style="color:rgb(153,153,153); line-height:18px; text-decoration:none">API Level 1</a> </div> </div> <div style="line-height:23px; margin-top:0.5em; margin-right:0.25em; margin-bottom:0.5em; margin-left:0.25em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <p style="line-height:1.3em; margin-top:0px; margin-bottom:1em; padding-top:0px; padding-bottom:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> </p> <p style="line-height:1.3em; margin-top:1em; margin-right:1em; margin-bottom:1em; margin-left:1em; padding-top:0px; padding-bottom:0px; padding-left:0.5em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:4px; border-style:initial; border-color:initial; border-left-style:solid; border-left-color:rgb(255,207,0); border-top-color:rgb(255,207,0); border-right-color:rgb(255,207,0); border-bottom-color:rgb(255,207,0)"> <span style="line-height:23px">This constructor is deprecated.</span><br style="line-height:23px"> This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace"><a rel="nofollow" href="http://developer.android.com/reference/android/app/LoaderManager.html" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">LoaderManager</a></code>with a<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace"><a rel="nofollow" href="http://developer.android.com/reference/android/content/CursorLoader.html" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">CursorLoader</a></code>.</p> <div style="line-height:1em; margin-top:0.25em; margin-right:0px; margin-bottom:0.75em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <p style="line-height:1.3em; margin-top:0.5em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> Constructor the enables auto-requery.</p> </div> </div> <span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <h4 style="line-height:25px; margin-top:1.25em; margin-right:0px; margin-bottom:0.65em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:1.1em; background-color:rgb(226,226,226); color:rgb(58,58,58)"> <span style="line-height:23px; font-size:0.9em">public</span><span style="line-height:25px; margin-right:2px">SimpleCursorAdapter</span><span style="line-height:23px; font-size:0.9em">(<a rel="nofollow" href="http://developer.android.com/reference/android/content/Context.html" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">Context</a>context, int layout,<a rel="nofollow" href="http://developer.android.com/reference/android/database/Cursor.html" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">Cursor</a>c,<a rel="nofollow" href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">String[]</a>from, int[] to, int flags)</span> </h4> <div style="line-height:18px; margin-top:-25px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:7px; padding-bottom:0px; padding-left:7px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:0.8em; color:rgb(153,153,153); float:right"> <div style="line-height:18px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> Since:<a rel="nofollow" href="http://developer.android.com/guide/appendix/api-levels.html#level11" style="color:rgb(153,153,153); line-height:18px; text-decoration:none">API Level 11</a> </div> </div> <div style="line-height:23px; margin-top:0.5em; margin-right:0.25em; margin-bottom:0.5em; margin-left:0.25em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <div style="line-height:1em; margin-top:0.25em; margin-right:0px; margin-bottom:0.75em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <p style="line-height:1.3em; margin-top:0.5em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> Standard constructor.</p> </div> <div style="line-height:23px; margin-top:0.5em; margin-right:1em; margin-bottom:0.5em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <h5 style="line-height:23px; margin-top:1em; margin-right:0px; margin-bottom:0.65em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:1em; color:rgb(58,58,58)"> Parameters</h5> <table style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:1em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:1em; border-collapse:collapse; empty-cells:show"><tbody style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <tr style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial; font-style:italic"> context</th> <td style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial"> The context where the ListView associated with this SimpleListItemFactory is running</td> </tr> <tr style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial; font-style:italic"> layout</th> <td style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial"> resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to"</td> </tr> <tr style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial; font-style:italic"> c</th> <td style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial"> The database cursor. Can be null if the cursor is not available yet.</td> </tr> <tr style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial; font-style:italic"> from</th> <td style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial"> A list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet.</td> </tr> <tr style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial; font-style:italic"> to</th> <td style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial"> 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. Can be null if the cursor is not available yet.</td> </tr> <tr style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial; font-style:italic"> flags</th> <td style="line-height:23px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial"> Flags used to determine the behavior of the adapter, as per<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace"><a rel="nofollow" href="http://developer.android.com/reference/android/widget/CursorAdapter.html#CursorAdapter(android.content.Context,%20android.database.Cursor,%20int)" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">CursorAdapter(Context, Cursor, int)</a></code>.</td> </tr> </tbody></table> </div> </div> 再看看那个CursorAdapter吧。它的列表中元素要求是Cursor,这东西与DB有关,不过最简单的DB就是通讯簿。先从Contacts.People入手吧,同样修改代码:</div> <div style="line-height:25px"> <div style="line-height:25px"> <span style="color:#808080; line-height:25px">//CursorAdapterdemo</span><br style="line-height:25px"> CursormCursor=this.getContentResolver().query(Contacts.People.CONTENT_URI,null,null,null,null);<br style="line-height:25px"> SimpleCursorAdapteradapter=newSimpleCursorAdapter(this,R.layout.list_row,mCursor,newString[]...{Contacts.People.NAME},newint[]...{R.id.item});<br style="line-height:25px"><br style="line-height:25px"> 因为单纯的CursorAdapter是抽象类,所以我用的是它的子类SimpleCursorAdapter,很好理解,先用ContentResolver查询通讯簿得到一个游标,然后告诉SimpleCursorAdapter要用其中的People.NAME作为显示项来构造出一个adapter即可。<br style="line-height:25px"><br style="line-height:25px"> 现在的onListItemClick也不一样了,如下:<br style="line-height:25px"><br style="line-height:25px"> protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid)<br style="line-height:25px"> ...{<br style="line-height:25px"> TextViewtxt=(TextView)this.findViewById(R.id.text);<br style="line-height:25px"> Cursorc=(Cursor)l.obtainItem(position);<br style="line-height:25px"> txt.setText("SEL="+c.getString(c.getColumnIndex(Contacts.People.NUMBER)));<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> 这里同样是先用obtainItem取到游标,然后用从记录中取出想要的字段显示即可。在做这个例子时,因为权限的问题我们还得修改一下AndroidManifest.xml文件,让我们的应用可以访问到通讯簿:<br style="line-height:25px"><br style="line-height:25px"> <manifestxmlns:android="http://schemas.android.com/apk/res/android"<br style="line-height:25px"> package="cn.sharetop.android.hello.two"><br style="line-height:25px"> <uses-permissionid="android.permission.READ_CONTACTS"/><br style="line-height:25px"> <applicationandroid:icon="@drawable/icon"></div> </div> </wbr>