0

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);
   }
 }
Community
  • 1
  • 1
AlexAndro
  • 1,918
  • 2
  • 27
  • 53

1 Answers1

1

Change below line from:

ImageView image = (ImageView)findViewById(R.id.btn);

to

ImageView image = (ImageView)view.findViewById(R.id.btn);
Maneesh
  • 6,098
  • 5
  • 36
  • 55
  • Your solution seems to work fine :D Could you tell me if I could extend also `CursorAdapter` instead of `SimpleAdapter`? I mean if the only change I have to do is in the adapter's constructor? `new SimpleCursorAdapter(this, R.layout.item_list, cursor, FROM, TO);` Thanks! – AlexAndro Dec 06 '11 at 11:38
  • As per my understanding you can extends the class MySimpleAdapter with SimpleCursorAdapter and then rewrite the rest of the same thing, try if this help you. – Maneesh Dec 06 '11 at 12:00