我希望我的列表项仍能回复onListItemClick()
但另外我希望在raw中有一个额外的图像来拥有它自己的点击事件。为图像定义onClickListener
的经典方法不是工作。什么I have found is that I have to define my own listadapter and override getView()
method。我已经锁定了定义新的适配器类,我从未使用过自定义适配器。我想浏览选项卡,onListItemClick()转到选项卡1并在ImageClick转到选项卡2.它适用于onListItemClick()
但是当我为图像添加第二个单击事件时,应用程序在MySimpleAdapter.getView处与NullPointerException崩溃()。任何帮助将不胜感激。谢谢。
public class ListaActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
MySimpleAdapter adapter = new MySimpleAdapter(this, elem, R.layout.list_item,
new String[]{"name","number","address"}, new int[]{R.id.name,R.id.num,R.id.adr});
setListAdapter(adapter);
}
public class MySimpleAdapter extends SimpleAdapter{
Context context;
//Activity activity;
public MySimpleAdapter(Context context, ArrayList<HashMap<String, String>> elements,int layout, String [] from, int [] to ){
super(context,el,layout,from,to);
this.context = context;
//this.activity = (Activity) context;
}
@Override
public View getView(int position,View convertView, ViewGroup parent){
View view = super.getView(position, convertView, parent);
ImageView image = (ImageView)findViewById(R.id.btn);
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int tab=2;
MyTabActivity.switchToTab(tab);
}
});
return view;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id){
int tab = 1;
MyTabActivity.switchToTab(tab);
}
}
答案 0 :(得分:1)
更改以下行:
ImageView image = (ImageView)findViewById(R.id.btn);
到
ImageView image = (ImageView)view.findViewById(R.id.btn);