我为listview编写了一个自定义适配器,但是当我尝试为列表项实现click事件时,我发现它没有响应,如果有人建议我解决,我会很高兴。
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"
/>
</...>
答案 0 :(得分:3)
查看this讨论是否有帮助......
答案 1 :(得分:1)
问题很简单。
在 setAdapter 调用之前,您无法调用 setOnItemClickListener ,因为在您设置适配器之前,ListView依赖于其子节点并且其子节点是未知的。
所以简单地这样做:
public class StatusListView extends ListView implements OnItemClickListener {
private StatusListAdapter adapter;
public StatusListView(Context context) {
super(context);
init();
}
private void init() {
// do other things
...
adapter = new StatusListAdapter(getContext(), R.layout.status_list_item, statuses);
// pay attention here
setAdapter(adapter);
setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
StatusListAdapter adapter = (StatusListAdapter) ((ListView) adapterView).getAdapter();
Status status = (Status) adapter.getItem(position);
Intent intent = new Intent(getContext(), CustomActivity.class);
intent.putExtra(C.extra_keys.status, status);
getContext().startActivity(intent);
}
}
通过这种方式,您可以启动并运行您的活动! 抱歉,我没有时间详细介绍Android源代码,如果我愿意,我会再次发布。
再见,伙计们, 亚历