今天用到listview的单击,长按事件。
this, ((TextView) arg1).setText()
this, ((TextView) arg1).getText()
代码如下:
1、Listview简述
A view that shows items in a vertically scrolling list. The items come from theListAdapter associated with this view.
简单来说就是,创建Listview,然后给数值就行了。
而这些数值来源有三种方式:ArrayAdapter、SimpleAdapter、SimpleCursorAdapter
第一种是最简单的一种Adapter,是字符串数值,只能在ListView显示出文本信息。
第二种是一种自定义的数据来源,要自定义布局方式,可以放置图片,按钮,文本之类的。
第三种数据来源于数据库。
本文为第一种方式,ArrayAdapter,其他两项的方法都差不多,主要是adapter不同。
2、使用ListView步骤
首先创建Listview组件,然后调用Listview.ArrayAdapter()方法,设置Adapter。
通过调用setOnItemClickListener()接口方法,设置“点击”listview某一项的监听事件。
通过调用setOnItemLongClickListener
()接口方法,设置“长按”listview某一项的监听事件。
需要说明的是,当设置匿名内部类new OnItemClickListener()时,eclipse不会自动载入复写函数,要点击左边的错误提示,然后Add unimplemented methods,才能载入复写函数onItemClick()。
[java]
view plain
copy
1. mylistview.setOnItemClickListener(new
2.
3. @Override
4. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long
5. // TODO Auto-generated method stub
6.
7. }
8. });
关于onItemClick方法,下面详细说说。(以下是官方的说明,参数命名不同而已,类型都是一样的。arg0=parent,arg1=view,arg2=position,arg3=id)
public abstract void onItemClick(AdapterView<?> parent,View view, int position, long id)
Since: API Level 1
Callback method to be invoked when an item in this AdapterView has been clicked.
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
//当点击listview某一项时,这个回调方法就会被调用。
Parameters
parent | The AdapterView where the click happened. |
view | The view within the AdapterView that was clicked (this will be a view provided by the adapter) |
position | The position of the view in the adapter. |
id | The row id of the item that was clicked. |
主要说说最后三个参数,
view——————是你点击的Listview的某一项的内容,来源于adapter。如用((TextView)arg1).getText(),可以取出点击的这一项的内容,转为string类型。
position————是adapter的某一项,如点击了listview第2项,而第2项对应的是adapter的第2个数值,那此时position的值就为1了。
如对应adapter的第3个数值,那此时position的值就为2了。
id———————id的值为点击了Listview的哪一项对应的数值,点击了listview第2项,那id就等于1。
注:这些数值都是从0开始的。
[java]
view plain
copy
1. /*author:conowen
2. * date:2012.2.26
3. */
4. package
5.
6. import
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15. import
16.
17. public class ListviewActivity extends
18. /** Called when the activity is first created. */
19. @Override
20. public void
21. super.onCreate(savedInstanceState);
22. setContentView(R.layout.main);
23. ListView lv = (ListView) findViewById(R.id.lv);
24. "windows", "linux", "ios", "android", "wp7",
25. "Symbian" };// 定义adapter
26. new ArrayAdapter<String>(this,// 把adapter绑定到listview里面
27. android.R.layout.simple_expandable_list_item_1, data));
28. // 点击事件
29. new
30.
31. @Override
32. public void onItemClick(AdapterView<?> arg0, View arg1, int
33. long
34. // TODO Auto-generated method stub
35. this, "你点击的是第" + arg3 + "项",
36. Toast.LENGTH_SHORT).show();
37. // 取出所点击的那一项的id
38.
39. }
40.
41. });
42. // 长按事件
43. new
44.
45. @Override
46. public boolean
47. int arg2, long
48. // TODO Auto-generated method stub
49. this,
50. ((TextView) arg1).getText(), Toast.LENGTH_LONG).show();
51. // 取出点击listview某一项的内容
52. return false;
53. }
54.
55. });
56. }
57. }