Android 带有自定义适配器和列表视图的 onItemClickListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11265743/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:35:25  来源:igfitidea点击:

onItemClickListener with custom adapter and listview

androidlistviewandroid-arrayadapteronitemclicklistener

提问by mwrazam

I am trying to set an onItemClickListener with a custom adapter and listview setup. Can't seem to get the listener working. I don't think I am setting it up properly. Any help is appreciated. Thanks very much.

我正在尝试使用自定义适配器和列表视图设置来设置 onItemClickListener。似乎无法让听众工作。我认为我没有正确设置它。任何帮助表示赞赏。非常感谢。

Adapter:

适配器:

public class ModuleAdapter extends ArrayAdapter<Module> {

Context context;
int layoutResourceId;
Module data[];

public ModuleAdapter(Context context, int layoutResourceId,
        Module data[]) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ModuleHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ModuleHolder();
        holder.modJapTitle = (TextView)row.findViewById(R.id.moduleJapTitle);
        holder.modEngTitle = (TextView)row.findViewById(R.id.moduleEngTitle);
        holder.modComp = (TextView)row.findViewById(R.id.moduleCompletion);
        holder.modRating = (RatingBar)row.findViewById(R.id.moduleScore);

        row.setTag(holder);
    }
    else
    {
        holder = (ModuleHolder)row.getTag();
    }

    Module module = data[position];
    holder.modJapTitle.setText(module.moduleJapaneseTitle);
    holder.modEngTitle.setText(module.moduleEnglishTitle);
    holder.modComp.setText(module.moduleCompletionRate);
    holder.modRating.setRating(module.moduleRating);

    return row;
}

static class ModuleHolder
{
    TextView modEngTitle;
    TextView modJapTitle;
    TextView modComp;
    RatingBar modRating;
}

}

}

Implementation:

执行:

ModuleAdapter moduleData = new ModuleAdapter(this, R.layout.module_box_item, module_data);
    ListView listView1 = (ListView)findViewById(R.id.moduleListContainer);
    listView1.setAdapter(moduleData);
    listView1.setCacheColorHint(Color.TRANSPARENT);
    listView1.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.v("Module Item Trigger", "Module item was triggered");
            Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();
        }
    });

Also here is the XML Layout for one of the single items in the list:

这里也是列表中单个项目之一的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/moduleBox"
android:id="@+id/moduleBoxSingle" >

<TextView
    android:id="@+id/moduleJapTitle"
    style="@style/moduleTitleJap" />
<TextView
    android:id="@+id/moduleCompletion"
    android:layout_above="@+id/moduleSepartor"
    style="@style/moduleCompletion" />
<View
    android:id="@+id/moduleSepartor"
    android:layout_below="@+id/moduleJapTitle"
    style="@style/moduleSeperator" />
<TextView
    android:id="@+id/moduleEngTitle"
    android:layout_below="@+id/moduleSepartor"
    style="@style/moduleTitleEng" />
<RatingBar
    android:id="@+id/moduleScore"
    android:layout_below="@+id/moduleSepartor"
    style="@style/moduleRating"
    android:layout_alignParentRight="true" />

</RelativeLayout>

回答by dreamtale

I think it's the similar problem like this.

我觉得它像类似的问题

Add below code to your TextViewin the XML

将以下代码添加到您TextView的 XML 中

android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"

and try again.

然后再试一次。

回答by coading fever

First of All for this you have to impliments the class with the following handler

首先,您必须使用以下处理程序来实现类

implements  OnItemClickListener 

then add a function as given below onItemClick()

然后添加一个如下所示的函数 onItemClick()

public void onItemClick(AdapterView parent, View v, int position, long id) {
  Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();   
}

And when we set the data adapter to the listviewthen add the following line of code

当我们将数据适配器设置为listview然后添加以下代码行

itemlist.setOnItemClickListener(this);

Hope this will work for you...

希望这对你有用......

回答by TuanTran

I have same issue and I fix it by do like:(refer from https://github.com/sephiroth74/HorizontalVariableListView/issues/33) add this line in root view in layout

我有同样的问题,我通过如下方式修复它:(参考https://github.com/sephiroth74/Horizo​​ntalVariableListView/issues/33)在布局的根视图中添加这一行

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/moduleBox"
android:id="@+id/moduleBoxSingle"
android:descendantFocusability="blocksDescendants" >

回答by Kumar Vivek Mitra

Try this...

尝试这个...

ListView lv = (ListView)findViewById(R.id.Your_ListView_Id);  // If it extends Activity.

OR

或者

ListView lv = getListView;  // If it extends ListActivity.
lv.setOnItemClickListener(this); // Make the Activity implement onItemClickListener
lv.setAdapter(your_adapter);

Edited part: Change made in Toast's Context, and Added SysOut

编辑部分:在 Toast 的上下文中进行了更改,并添加了 SysOut

// The method that will handle the Events.

// 处理事件的方法。

public void onItemClick(AdapterView parent, View v, int position, long id{
     Toast.makeText(Your_Activity_Name.this, "hello", Toast.LENGTH_SHORT).show();
     System.out.println("This is a Test");
}

CHECK YOU LOG.. DOES THE SYSOUT STATEMENT GETS PRINTED !!

检查您的日志......是否打印了 SYSOUT 语句!