问题描述
我想为使用 ArrayAdapter 创建的 ListView 提供 OnItemClickListener
I want to have an OnItemClickListener for a ListView I create using an ArrayAdapter
这是我用来创建它的代码:
This is the code I use to create it:
List<Comment> values = datasource.some_search("Wednesday","11");
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
如何实现 onItemClickListener?
How do I implement onItemClickListener?
谢谢!
我在 ArrayAdapter 和 ListView 中使用一串对象.
I am using in my ArrayAdapter and ListView a string of objects.
编辑 2:更多代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource = new CommentsDataSource(this);
datasource.open();
//check if database is populated if NOT, populate with txtToDb();
if (!datasource.isPopulated()) {
// Database is not populated so copy it from assets here
try {
txtToDb();
Log.i("Database", "Was not Populated");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("Database", "Was not populated: txtToDb(); failed");
}
} else {
Log.i("Database", "Populated");
}
//wat to show on screen:
List<Comment> values = datasource.search("Wednesday","11");
// Use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
编辑 3:XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add New"
android:onClick="onClick"/>
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete First"
android:onClick="onClick"/>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
推荐答案
使用 OnItemClickListener
Use OnItemClickListener
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String value = (String)adapter.getItemAtPosition(position);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});
当您单击一行时,会触发一个侦听器.所以你在列表视图上 setOnClickListener
并使用匿名内部类 OnItemClickListener
.
When you click on a row a listener is fired. So you setOnClickListener
on the listview and use the annonymous inner class OnItemClickListener
.
您还可以覆盖 onItemClick
.第一个参数是一个适配器.第二个参数是视图.第三个参数是位置(列表视图项的索引).
You also override onItemClick
. The first param is a adapter. Second param is the view. third param is the position ( index of listview items).
使用你获得物品的位置.
Using the position you get the item .
根据您的评论,我假设您需要设置适配器或列表视图
Edit : From your comments i assume you need to set the adapter o listview
所以假设你的活动扩展了 ListActivtiy
So assuming your activity extends ListActivtiy
setListAdapter(adapter);
或者如果你的活动类扩展了活动
Or if your activity class extends Activity
ListView lv = (ListView) findViewById(R.id.listview1);
//initialize adapter
lv.setAdapter(adapter);
这篇关于OnItemClickListener 为 ListView 使用 ArrayAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!