1

I have a ListView which after clicking on an item I want it to be permanently selected, so some other action 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);
                    }
                });
}

2 Answers 2

0

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?

1
  • Sounds like you are right with that, i'll scroll and then go back to where I was and it will be a different item selected. It can't really be a long press though, it needs to be clicked, the background changes and then I can either delete or start a new intent depending on the next button press.
    – SamRowley
    Feb 4, 2011 at 13:59
0

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);
            }                
        }

    });        
2
  • Hey thanks for the answer, I don't really follow. I am about to add my code to the question, could you tell me how your answer could relate to my problem?
    – SamRowley
    Feb 4, 2011 at 14:36
  • Android is recycling the views. In your case, you'd want to extend SimpleAdapter and provide an implementation of setViewText() and setViewImage() that sets the background color on the view. Feb 15, 2011 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.