Select multiple items from listview and change color of selected item only

后端 未结 7 722
面向向阳花
面向向阳花 2020-12-30 13:40

I want to make a view where I can select multiple items from listview and also side by side am changing the color of selected list item and saving that item into my arraylis

7条回答
  •  既然无缘
    2020-12-30 14:09

    The solution (idea) from Jason Robinson is good, but there is an error! You can not remove item from ArrayList by it's value!!!
    Method ArrayList.remove(int) takes item's index as parametr!
    If you want to use an ArrayList<Integer>, you need to find index ot item you want to remove. For example:

    private int getIndex(int value) {
        int index = -1;
    
        for(int i=0; i

    Or use method ArrayList.indexOf(Object) to obtain index.

    And now, how to get reference to your adapter from FragmenList or ListActivity. Simply use class variable:

    public class SimpleFragment extends ListFragment {
        private MyCustomAdapter mAdapter;
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
            mAdapter = new MyCustomAdapter(getActivity(), R.layout.row_layout);
            setListAdapter(mAdapter);
        }
    
        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            // now you have an access to your adapter through class variable mAdapter
        }
    }
    

提交回复
热议问题