【问题标题】:onItemClickListener with custom adapter and listview带有自定义适配器和列表视图的 onItemClickListener
【发布时间】:2012-06-29 16:42:53
【问题描述】:

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

适配器:

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;
}

}

实施:

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();
        }
    });

这里也是列表中单个项目之一的 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>

【问题讨论】:

  • 一切看起来都不错。也许这是一个UI问题?你为什么不写信给 logcat 而不是弹出一个 Toast。只是为了 100% 确定您的听众没有被呼叫。
  • 你可以在这里发布你的布局xml吗?
  • 您好,感谢您的回复。我已经添加到写入 Logcat 的行中。我还添加了用于每个列表项的 XML 布局。

标签: android listview android-arrayadapter onitemclicklistener


【解决方案1】:

我认为这与this类似的问题。

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

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

然后再试一次。

【讨论】:

  • 这成功了!太感谢了。节省了很多头发。 :)
【解决方案2】:

首先,您必须使用以下处理程序来隐含该类

implements  OnItemClickListener 

然后添加下面给出的函数 onItemClick()

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

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

itemlist.setOnItemClickListener(this);

希望这对你有用...

【讨论】:

  • 您好,感谢您的回复。我试了一下,但结果与我上面的原始代码相同。也许我的代码有其他问题没有触发那个监听器?
【解决方案3】:

我有同样的问题,我通过 do like 解决它:(参考https://github.com/sephiroth74/HorizontalVariableListView/issues/33) 在布局的根视图中添加这一行

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

【讨论】:

    【解决方案4】:

    试试这个...

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

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

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

    // 将处理事件的方法。

    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");
    }
    

    检查您的日志.. 是否打印 SYSOUT 声明!!

    【讨论】:

    • 您好,感谢您的留言。不幸的是,这也不起作用。 Toast 消息仍未触发。
    • 稍等......试试这个.. Toast.make Toast.makeText(Your_Activity_Name.this, "hello", Toast.LENGTH_SHORT).show();
    猜你喜欢
    • 2015-07-20
    • 2015-03-01
    • 1970-01-01
    • 2015-10-15
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多