Search code examples
androidlistviewcell

ListView : how can I change a cell after a click?


I have a list view, containing different kinds of cells and I use an ArrayAdapter to do so.

private class MyAdapter extends ArrayAdapter<String>
{   
    public MyAdapter(Context context, int resource, int textViewResourceId, String[] objects)
    {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View view, ViewGroup parent)
    {
        View viewCE = inflater.inflate(R.layout.my_cell, parent, false);
        ((TextView) viewCE.findViewById(R.id.txt_cell)).setText(adapter.getItem(position));
        view = viewCE;
        return view;
    }
}

my_cell contains an ImageView, and what I want to do is to change the image of this ImageView when the user clicks the cell.

I already added to my code a private class implementing OnItemClickListener with its method onItemClick() but then I've absolutely no idea of what to do...

Thanks for your help.


Solution

  • in onItemCLick callback, the parameter view is exactly the view that contains your imageView, so you can find it by id then change it, sample codes are as below:

    list.setOnItemClickListener(new OnItemClickListener() {
    
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
            ImageView image = ((ImageView) viewCE.findViewById(R.id.your_image_id));
            //do something by yourself
        }
    }