0

Alright, so I know this question has been asked before on this website but quite frankly, I haven't had any results from the solutions I have seen. For example, this post: highlighting the selected item in the listview in android suggests that all you need to do is to set a choiceMode to the ListView. I've tried this and it doesn't work at all.

In my code I have a listener for the ListView (onListItemClick) and each time it is triggered, I have something like this...getListView().setItemChecked(position, true), and on top of that I have an XML selector which I have attached to my ListView...

<ListView                       
  android:id="@id/android:list"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:scrollbars="none"
  android:layout_weight="1.5"
  android:choiceMode="singleChoice"
  android:listSelector="@drawable/bounty_list_selector"/>

Am I missing something here? Does setItemChecked() have no influence on the ListView's selector?

Community
  • 1
  • 1
Alex Fu
  • 5,509
  • 3
  • 31
  • 40
  • Are you just trying to get the selector graphic to display on clicking a row? (it won't if something inside the row has its own onClickListener) – FunkTheMonk Nov 24 '11 at 22:56
  • 1
    I think the idea of android:listSelector is to deal with the list selection (moving in the list using a dpad), it doesn't affect checked rows. – aromero Nov 24 '11 at 23:03
  • @FunkTheMonk, im trying to get an item in the list to "highlight" or basically change background colors when clicked, however i require only one item to be "checked" at a time. – Alex Fu Nov 25 '11 at 02:28

1 Answers1

0

I have had a similar issue, and solved it via code. It might not be the brightest nor the most efficient solution, but works around ListView drawing issues and so far seems to be working well.

The activity file:

private ListView listView;
private View selectedView;
private int selectedPosition;
private MyAdapter arrayAdapter;

...
// set a state machine for when a list item is clicked
    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (selectedPosition == -1) {
                // no item is selected, mark as selected
                selectedView = view;
                view.setBackgroundResource(R.drawable.bg_selected);
                selectedPosition = position;
            } else if (selectedPosition == position) {
                // the same item is selected as the previous one, deselect
                selectedView = null;
                view.setBackgroundResource(R.drawable.bg_unselected);
                selectedPosition = -1;
            } else {
                // another item is selected, deselect the previous and select the new one
                selectedView.setBackgroundResource(R.drawable.bg_unselected);
                selectedView = view;
                view.setBackgroundResource(R.drawable.bg_selected);
                selectedPosition = position;
            }

            arrayAdapter.setSelectedPosition(selectedPosition);
        }
    });

The adapter code:

public class MyAdapter extends ArrayAdapter<MyObject> {
    private int selectedPosition;

    private static class ViewHolder {
        RelativeLayout layout;
        ...
    }

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_row, null);
            holder = new ViewHolder();
            holder.layout = (RelativeLayout) convertView.findViewById(R.id.rl_item);
            ...

            convertView.setTag(holder);
        }
        else
            holder = (ViewHolder) convertView.getTag();

        ...
        if (position == selectedPosition)
            holder.layout.setBackgroundResource(R.drawable.bg_selected);
        else
            holder.layout.setBackgroundResource(R.drawable.bg_unselected);
        ...

        return convertView;
     }

     ...
}

Hope it helps!

jcxavier
  • 2,232
  • 1
  • 15
  • 24