Android入门逆引手册 - 10 列表框(ListView)的使用

这里介绍android.widget.ListView类的使用方法。 

 77-1

 

 

 

 

 

 

 

● 设置Adapter,调用setAdapter()方法。
● 追加Item被点击时候的处理,调用setOnItemClickListener()方法。
● 追加Item被选择时候的处理,调用setOnItemSelectedListener()方法。

例程源码(Java)

01.ArrayAdapter<String> adapter = new ArrayAdapter<String>(
02.this,
03.android.R.layout.simple_list_item_1);
04. 
05.adapter.add("red");
06.adapter.add("green");
07.adapter.add("blue");
08. 
09.ListView listView = (ListView) findViewById(id.listView);
10. 
11.listView.setAdapter(adapter);
12. 
13.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
14.@Override
15.public void onItemClick(
16.AdapterView<?> parent,
17.View view,
18.int position,
19.long id) {
20.ListView listView = (ListView) parent;
21.Log.v("Test""id = " + id + "("
22.+ listView.getItemAtPosition(position).toString() + ")");
23.}
24.});
25. 
26.listView.setOnItemSelectedListener(new OnItemSelectedListener() {
27.@Override
28.public void onItemSelected(
29.AdapterView<?> parent,
30.View view,
31.int position,
32.long id) {
33.ListView listView = (ListView) parent;
34.Log.v("Test""id = " + id + "("
35.+ listView.getSelectedItem().toString() + ")");
36.}
37.@Override
38.public void onNothingSelected(AdapterView<?> parent) {
39.}
40.});

例程源码(Resource)

1.<ListView android:id="@+id/listView"
2.android:layout_width="fill_parent"
3.android:layout_height="fill_parent" />

参考资源
● ListView | Android Developers
● ArrayAdapter | Android Developers
● R.layout | Android Developers


 

posted on 2009-07-27 22:58  mockups  阅读(4000)  评论(0编辑  收藏  举报