[英]Android: On Item Click Listener for List View Base Adapter implementation
Can someone point give me an example of a On item click listener used for a list view with base adapter? 有人可以给我提供一个示例的单击项侦听器的示例,该侦听器用于带有基本适配器的列表视图吗?
My list view contains two text views and a check box. 我的列表视图包含两个文本视图和一个复选框。 I want to create an on item click listener so that when a item (or row) is pressed, it ticks the corresponding row check box. 我想创建一个项目单击侦听器,以便在按下一个项目(或行)时,它勾选相应的行复选框。
I tried to Google it but could not find any examples of it 我尝试使用Google,但找不到任何示例
Thank you 谢谢
So, the adapter does not really matter, but i believe what you are trying to do is pretty simple, first you need to get a refrence to your ListView
which i will refer to as listView
因此,适配器并不重要,但是我相信您要尝试的操作非常简单,首先您需要获得ListView
引用,我将其称为listView
after setting your adapter you can use setOnItemClickListener
to create the click action, 设置适配器后,您可以使用setOnItemClickListener
创建点击操作,
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public boolean onItemClick(AdapterView<?> parent, View view, int position, long id) {
//here you can use the position to determine what checkbox to check
//this assumes that you have an array of your checkboxes as well. called checkbox
checkBox[position].setChecked(!checkBox.isChecked());
}
});
It's actually quite simple, after you perform the action "setAdapter" You have to do this: 实际上,这很简单,在执行操作“ setAdapter”之后,您必须执行以下操作:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You click on position:"+position, Toast.LENGTH_SHORT).show();
}
});
The following code works correctly: 下面的代码正常工作:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
Toast.makeText(getApplicationContext(),
"You click on position:"+position, Toast.LENGTH_SHORT).show();
}
});
But when I clicked items
then it goes to other activities. 但是,当我单击items
它将转到其他活动。 What should I do? 我该怎么办?
You can access all the elements of your listview row using View parameter of OnItemClick() method. 您可以使用OnItemClick()方法的View参数访问listview行的所有元素。 eg 例如
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
// second parameter View view entire row object that is clicked - in your case
// two text views and the checkbox
// supposing your checkbox is set up with an id in the xml as idchkbox
// You can access it by findview by id and set it true or false;
android.widget.CheckBox chkbox = view.findViewbyId(R.id.idchkbox);
chkbox.setChecked = true;
return;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.