gpt4 book ai didi

android - 带有自定义适配器和 ListView 的 onItemClickListener

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:27 26 4
gpt4 key购买 nike

我正在尝试使用自定义适配器和 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>

最佳答案

我认为这是与 this 类似的问题.

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

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

再试一次。

关于android - 带有自定义适配器和 ListView 的 onItemClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11265743/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com