我正在尝试使用自定义适配器和listview设置来设置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>
答案 0 :(得分:18)
我认为这是类似this的问题。
将以下代码添加到XML
中的TextView
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
再试一次。
答案 1 :(得分: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);
希望这对你有用......
答案 2 :(得分:2)
我有同样的问题,我通过这样做来解决:(参考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" >
答案 3 :(得分:1)
试试这个......
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);
编辑部分:在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");
}
检查你的日志...... SYSUT声明是否已打印!!