Java Android:用于列表视图基本适配器实现的项目单击侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/24499111/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: On Item Click Listener for List View Base Adapter implementation
提问by user3364963
Can someone point give me an example of a On item click listener used for a list view with base adapter?
有人可以给我一个用于带有基本适配器的列表视图的 On item click listener 的示例吗?
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
我试着用谷歌搜索但找不到任何例子
Thank you
谢谢
采纳答案by Sean C.
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 ListViewwhich i will refer to as listView
所以,适配器并不重要,但我相信你想要做的很简单,首先你需要得到你的参考ListView,我将称之为listView
after setting your adapter you can use setOnItemClickListenerto 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());
        }
    });
回答by Yuval
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();
        }
    });
回答by KBH Tech
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 itemsthen it goes to other activities. What should I do? 
但是当我点击items它时,它会转到其他活动。我该怎么办?
回答by Ganesan R
You can access all the elements of your listview row using View parameter of OnItemClick() method. e.g.
您可以使用 OnItemClick() 方法的 View 参数访问列表视图行的所有元素。例如
    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;
    });

