开发者

Selected item ListView Background change

开发者 https://www.devze.com 2023-02-08 19:05 出处:网络
I have a ListView which after clicking on an item I want it to be permanently selected, so some other actio开发者_运维技巧n can be taken depending on what button is pressed next. A bit like a RadioBox

I have a ListView which after clicking on an item I want it to be permanently selected, so some other actio开发者_运维技巧n can be taken depending on what button is pressed next. A bit like a RadioBox but within the list view. So when pressed, the background stays yellow and I keep a store of which item is selected. At the moment I have it when it is clicked the the background changes, but haveing weird behavior with when selected and I scroll the ListView the selected item changes.

Code I have:

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        for (int i = 0; i < titles.size(); i++){
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("name", titles.get(i));
            mylist.add(map);
        }

        SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.listcell,
                new String[] {"name"}, new int[] {R.id.txtItemName}); 

        ListView listView1 = (ListView) findViewById(R.id.ListView01); 
        listView1.setAdapter(mSchedule); 
        listView1.setTextFilterEnabled(true);
        listView1.setOnItemClickListener(new OnItemClickListener() { 
                    @Override
                    public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
                        Log.v("Test", v.toString());
                        v.setBackgroundResource(R.drawable.newbackground);
                    }
                });
}


The views in a ListView get recycled so if the selected view goes off the screen it is probably being re-used for one of the current visible items. That might be the weird behavior you are describing? If you want to do multiple potential actions based on a item selection usually that is done either by a long-press + showing context menu.

What is the weird behavior?


Track the selected items in a HashSet. Override the SimpleAdapter.getView() method to assign the background resource based on the selectedItems HashSet.

    final HashSet<String> selectedItems = new HashSet<String>();
    SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.listcell,new String[] {"name"}, new int[] {R.id.txtItemName}) {
        @Override
        public View getView(int position, View v, ViewGroup parent) {
            if(v!= null) {
                String title = titles.get((int)this.getItemId(position));
                if(selectedItems.contains(title)) {
                    v.setBackgroundResource(R.drawable.newbackground);
                } else {
                    v.setBackgroundResource(0);
                }
            }
            return super.getView(position, v, parent);
        }
    };
    ListView listView1 = (ListView)findViewById(R.id.listView1);        
    listView1.setAdapter(mSchedule); 
    listView1.setTextFilterEnabled(true);        
    listView1.setOnItemClickListener(new OnItemClickListener() { 
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {      
            String title = titles.get((int)id);
            if(selectedItems.contains(title)) {
                v.setBackgroundResource(0);
                selectedItems.remove(title);

            } else {
                v.setBackgroundResource(R.drawable.newbackground);
                selectedItems.add(title);
            }                
        }

    });        
0

精彩评论

暂无评论...
验证码 换一张
取 消