6

我正在尝试使用自定义适配器和列表视图设置来设置 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>
4

4 回答 4

17

我认为这是类似的问题

TextView在 XML 中添加以下代码

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

然后再试一次。

于 2012-06-30T06:53:07.890 回答
4

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

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

希望这对你有用...

于 2012-06-29T16:58:17.980 回答
2

我有同样的问题,我通过这样做来解决它:(参考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" >
于 2016-09-20T09:45:53.517 回答
0

试试这个...

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 声明!

于 2012-06-30T03:19:48.423 回答