setOnItemClickListener 不响应自定义 ListView
我为列表视图编写了一个自定义适配器,但是当我尝试为列表项实现单击事件时,我发现它没有响应,如果有人建议我一个解决方案,我会很高兴。
public class TourList extends ListActivity {
....
setContentView(R.layout.tourlist);
.....
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
//i couldn't reach here
Log.v(TAG,"did u get me");
}
});
adap = new MyAdapter(TourList.this,mylist);
getListView().setAdapter(adap);
我的自定义适配器是
private class MyAdapter extends BaseAdapter {
ArrayList<HashMap<String,String>> elements;
Context ctx;
public MyAdapter(Context context, ArrayList<HashMap<String,String>> mylist) {
this.elements=mylist;
this.ctx=context;
}
public boolean isEnabled(int position){
return true;
}
@Override
public int getCount() {
return elements.size();
}
@Override
public Object getItem(int position) {
return elements.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.rowfor_tourlist, null);
}
TextView in = (TextView)v.findViewById(R.id.intro);
TextView du = (TextView)v.findViewById(R.id.duration);
TextView pf = (TextView)v.findViewById(R.id.price);
TextView pn = (TextView)v.findViewById(R.id.product);
WebView wv=(WebView)v.findViewById(R.id.photo);
in.setText(Html.fromHtml(mylist.get(position).get("Intro")));
du.setText(mylist.get(position).get("Duration"));
pf.setText(mylist.get(position).get("Price"));
pn.setText(mylist.get(position).get("Product"));
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(mylist.get(position).get("ImageURL"));
return v;
}
}//class
,我的tourlist.xml 文件看起来像
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
....
>
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
android:cacheColorHint="#00000000"
android:layout_weight="1"
/>
</...>
I wrote a custom Adapter for a listview ,but when i tried implement click event for list item ,i found that it was not responding ,I will be glad if someone suggest me a solution.
public class TourList extends ListActivity {
....
setContentView(R.layout.tourlist);
.....
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
//i couldn't reach here
Log.v(TAG,"did u get me");
}
});
adap = new MyAdapter(TourList.this,mylist);
getListView().setAdapter(adap);
and my custom adapter is
private class MyAdapter extends BaseAdapter {
ArrayList<HashMap<String,String>> elements;
Context ctx;
public MyAdapter(Context context, ArrayList<HashMap<String,String>> mylist) {
this.elements=mylist;
this.ctx=context;
}
public boolean isEnabled(int position){
return true;
}
@Override
public int getCount() {
return elements.size();
}
@Override
public Object getItem(int position) {
return elements.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.rowfor_tourlist, null);
}
TextView in = (TextView)v.findViewById(R.id.intro);
TextView du = (TextView)v.findViewById(R.id.duration);
TextView pf = (TextView)v.findViewById(R.id.price);
TextView pn = (TextView)v.findViewById(R.id.product);
WebView wv=(WebView)v.findViewById(R.id.photo);
in.setText(Html.fromHtml(mylist.get(position).get("Intro")));
du.setText(mylist.get(position).get("Duration"));
pf.setText(mylist.get(position).get("Price"));
pn.setText(mylist.get(position).get("Product"));
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(mylist.get(position).get("ImageURL"));
return v;
}
}//class
and my tourlist.xml file looks like
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
....
>
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
android:cacheColorHint="#00000000"
android:layout_weight="1"
/>
</...>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题很简单。
您不能在 setAdapter 调用之前调用 setOnItemClickListener,因为 ListView 依赖于其子级,并且在您设置适配器之前,其子级是未知的。
因此,只需这样做:
这样您就可以启动并运行您的活动!
抱歉,我没有时间详细介绍 Android 源代码,如果愿意,我会再次发布。
再见,伙计们,
亚历克斯
The question is really simple.
You cannot call setOnItemClickListener before setAdapter call because ListView relies on its children and its children are unknown until you set the adapter.
So simply do like this:
In this way you will have your event up and running !
Sorry but I didn't time to go in detail in Android source code, if I will I will post again.
Bye guys,
Alex