Android: Dynamically change Image in Listview

前端 未结 5 2008
傲寒
傲寒 2021-02-04 07:08

I have a listview defined by the following xml. I need to toggle the image in the list during runtime when the user clicks on any row. How can I achieve this? A

5条回答
  •  醉梦人生
    2021-02-04 07:51

    This can be done so easily, you need to just look at on the position of list item that was clicked by user. A well described complete demo code is as below_

     public class ChangeImageInListDynamically extends Activity {
    /**
     * Adapter for your data. 
     */
    ImageAdapter adpAR;
    ArrayList itemList;
    ListView mListView;
    ImageAdapter mAdapter;
    
    /**
     * Indicate which item is currently selected.  
     * Its default selected item is '0' e.g., 1st item. 
     */
    private int selectedPosition=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        itemList = new ArrayList();
        for (int i = 0; i < 11; i++) {
            itemList.add("Item - "+i);
        }
    
        mListView = (ListView)findViewById(R.id.data_list);
    
        //Initialize your adapter
        mAdapter = new ImageAdapter(nChangeImageInListDynamically.this, itemList);
    
        //set adapter to your list.
        mListView.setAdapter(mAdapter);
    
        //set list click listener.
        mListView.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView av, View view, int position, long id) {
                //your image that need to change dynamically.
                ImageView imageView = (ImageView) view.findViewById(R.id.select);
    
                //position of clicked item.  
                selectedPosition=position;
    
                //change image of respective image.
                imageView.setImageResource(R.drawable.selected);
    
                //notify your adapter to update your list.  
                adpAR.notifyDataSetChanged();
            }
        });
    
    }
    
    static class ViewHolder {
        TextView title;
        ImageView img;                  
    }   
    
    /**
     * Your ImageAdapter.
     */
    public class ImageAdapter extends BaseAdapter {
    
        ArrayList dataList;
        Context context;
    
        public ImageAdapter(Context context, ArrayList arrayList) {
            this.context = context;
            this.dataList = arrayList;
    
        }
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return dataList.size();;
        }
    
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
    
            final ViewHolder holder;
            Log.d("posiiton ImageAdapater : ",""+position);
            if (row == null) { 
                LayoutInflater inflater = ChangeImageInListDynamically.this.getLayoutInflater();
                row = inflater.inflate(R.layout.listrow, null);
    
                holder = new ViewHolder();
                holder.title = (TextView) row.findViewById(R.id.text);
                holder.img = (ImageView) row.findViewById(R.id.select);
                row.setTag(holder);
            } else {
                holder = (ViewHolder) row.getTag();
            }
    
            //set titel text.
            holder.title.setText(dataList.get(position));
    
            //Change Image of selected item dynamically.
                 if(selectedPosition == position) {
                     //Item is selected.
                     holder.img.setImageResource(R.drawable.selected);
    
                 }else{
                     //Other non-selected items.
                     holder.img.setImageResource(R.drawable.dis_selected);
    
                 }
    
            return row;
    
        }
    
     }
    
    }
    

    I hope this will help to all :)

提交回复
热议问题