I want that my listitem to still respond to onListItemClick()
but additionally I want an extra image in a raw to has it's own click event.The classic way,defining onClickListener
for the image doesn't work. What I have found is that I have to define my own listadapter and override getView()
method. I have locked defining new adapter class, I have never used a custom adapter. I want to navigate through tabs, onListItemClick() go to tab 1 and on ImageClick go to tab 2. It works for onListItemClick()
but when I add the second click event for the image the app crashes with NullPointerException at MySimpleAdapter.getView(). Any help will be appreciated.Thanks.
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);
}
}
Change below line from:
ImageView image = (ImageView)findViewById(R.id.btn);
to
ImageView image = (ImageView)view.findViewById(R.id.btn);