简体   繁体   English

Android:在“项目”上单击“侦听器”以实现列表视图基本适配器实现

[英]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.

相关问题 使用适配器和数组列表时,我需要在回收视图中的项目上实现单击侦听器 class - I need to implementation click listener class on item in recycle view when using adapter and array list 如何进入listview适配器项目在android中单击侦听器片段? - How to go listview adapter item click listener to fragment in android? Android:具有基本适配器的自定义列表视图:如何在点击列表器中设置行项目? - Android: custom List view with base adapter: How to set on click listner for row items? 在列表视图中解析Json并将Click侦听器添加到每个列表项 - Parse Json in list view and add click listener to each list item 点击侦听器上的Android网格视图获取选定项 - Android grid view on click listener get selected item 带适配器的 Android 列表视图仅显示第一项(多次) - Android list view with adapter displays only the first item (several times) Android:项目列表单击JSON生成的列表视图-无法单击 - Android: item list click on JSON generated list view - unable to click 通过另一个列表视图的外部单击侦听器更改通知数据更改的列表视图视图项的颜色 - Changing the color of a listview view item that notifies data changes through an outer click listener of another list view Android工具在列表项上单击以通过自定义列表适配器将数据传递到新的活动 - Android implement on list item click to pass data to new Activity via custom list adapter 在单击侦听器上设置了按钮的Android Spinner项目 - Android Spinner Item With Button Set On Click Listener
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM