onItemClickListener与自定义适配器和ListView [英] onItemClickListener with custom adapter and listview
问题描述
我想设定一个自定义适配器和ListView设置一个onItemClickListener。似乎无法让听者工作。我不认为我正确设置它。任何帮助是AP preciated。非常感谢。
适配器:
公共类ModuleAdapter扩展ArrayAdapter<&模块GT; {上下文语境;
INT layoutResourceId;
模块数据[];公共ModuleAdapter(上下文的背景下,INT layoutResourceId,
模块数据[]){
超级(上下文,layoutResourceId,数据);
this.layoutResourceId = layoutResourceId;
this.context =背景;
this.data =数据;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看排= convertView;
ModuleHolder支架=无效; 如果(行== NULL)
{
LayoutInflater充气=((活动)上下文).getLayoutInflater();
行= inflater.inflate(layoutResourceId,父母,假); 持有人=新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(保持器);
}
其他
{
支架=(ModuleHolder)row.getTag();
} 模块模块=数据[位置]
holder.modJapTitle.setText(module.moduleJapaneseTitle);
holder.modEngTitle.setText(module.moduleEnglishTitle);
holder.modComp.setText(module.moduleCompletionRate);
holder.modRating.setRating(module.moduleRating); 返回行;
}静态类ModuleHolder
{
TextView的modEngTitle;
TextView的modJapTitle;
TextView的modComp;
的RatingBar modRating;
}
}
实施
ModuleAdapter moduleData =新ModuleAdapter(这一点,R.layout.module_box_item,module_data);
ListView控件ListView1的=(的ListView)findViewById(R.id.moduleListContainer);
listView1.setAdapter(moduleData);
listView1.setCacheColorHint(Color.TRANSPARENT);
listView1.setOnItemClickListener(新android.widget.AdapterView.OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
Log.v(模块项目触发器,模块项目被触发);
Toast.makeText(getApplicationContext(),你好,Toast.LENGTH_SHORT).show();
}
});
另外这里为列表中的单个项目之一的XML布局:
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
风格=@风格/ moduleBox
机器人:ID =@ + ID / moduleBoxSingle><的TextView
机器人:ID =@ + ID / moduleJapTitle
风格=@风格/ moduleTitleJap/>
<的TextView
机器人:ID =@ + ID / moduleCompletion
机器人:layout_above =@ + ID / moduleSepartor
风格=@风格/ moduleCompletion/>
<查看
机器人:ID =@ + ID / moduleSepartor
机器人:layout_below =@ + ID / moduleJapTitle
风格=@风格/ moduleSeperator/>
<的TextView
机器人:ID =@ + ID / moduleEngTitle
机器人:layout_below =@ + ID / moduleSepartor
风格=@风格/ moduleTitleEng/>
<的RatingBar
机器人:ID =@ + ID / moduleScore
机器人:layout_below =@ + ID / moduleSepartor
风格=@风格/ moduleRating
机器人:layout_alignParentRight =真/>< / RelativeLayout的>
我认为这是类似的问题,像<一个href=\"http://stackoverflow.com/questions/9765120/onlistitemclick-is-not-working-for-listview/9765368#9765368\">this.
在下面添加code到你的的TextView
在XML
的android:focusableInTouchMode =假
机器人:可点击=假
机器人:可聚焦=假
和重试。
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.
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 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>
I think it's the similar problem like this.
Add below code to your TextView
in the XML
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
and try again.
这篇关于onItemClickListener与自定义适配器和ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!