0

In protected void onListItemClick(ListView l, View v, int position, long id) method I use

lv.getChildAt(position-lv.getFirstVisiblePosition()).setBackgroundColor(getResources().getColor(android.R.color.darker_gray));

But background of two elements is changing ex. clicking first element (index 0) #0 and #8 change, clicking second item (index 1) #1 and #9 change, clicking 9th item (index 8) #8 and #1 change. What is going on?

----EDIT----

I used parameter v instead of lv.getChiledAt. It looks like that:

v.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));

It still works the same way. I do not know where the problem is: listview, adapter, onlistitemclick method? I use the simplest adapter:

lv = (ListView) findViewById(android.R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, android.R.id.text1, values);
lv.setAdapter(adapter);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

-----EDIT2---- Ok, one more thing. After clicking element i add its value to a list. And it works fine: to the list there is added only one element - however there are two positions highlighted. So the problem is somewhere with view... i can write my custom adapter, but it seems pretty complicated in this case. I would rather understand why this simple solution doesn't work.

Malvinka
  • 1,185
  • 1
  • 15
  • 36
  • Are both elements visible at the same time or do you have to scroll to see both changed elements? – JstnPwll Nov 10 '14 at 20:01
  • per Nitesh, Are you using the view holder pattern for listview adapter.? – codeMagic Nov 10 '14 at 20:07
  • I need to scroll to see the second - I am not able to see them both at the same time. I've got 7 items visible at the same time (index 0-6) – Malvinka Nov 10 '14 at 20:18
  • The simplest solution I found here, in @CommonsWare answer: http://stackoverflow.com/questions/9729517/showing-the-current-selection-in-a-listview – Malvinka Nov 11 '14 at 12:17

3 Answers3

1

Listview reuses the created views for better performance. When you change the background color of a view in the list, you will see it when you scroll the list. Because same view comes again with different data.

TurKorsaN
  • 53
  • 1
  • 6
0

Try this. in your custom adapter

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

if (convertView == null) {
    convertView = mInflater.inflate(R.layout.your_layout, null);

    holder = new ViewHolder();
    holder.text = (TextView) convertView.findViewById(R.id.text);

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

// Do whatever you want to do with your views
holder.text.setText("Position " + position);

if(highlightedPosition == position) {
convertView.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
}
else
{
convertView.setBackgroundColor(getResources().getColor(android.R.color.black));
}

return convertView;

}

private static class ViewHolder {
public TextView text;
// Your other views goes here

}

public void highlightPosition(int highlightedPosition){
this.highlightedPosition = highlightedPosition;
notifyDatasetChanged();
}

Nitesh
  • 3,868
  • 1
  • 20
  • 26
  • Is really custom adapter neccessary in this case? It looks pretty simple - highlighting checked item... – Malvinka Nov 10 '14 at 21:11
0

Create local View class instead of ViewHolder:

ItemView class:

private static /* or not */ class ItemView extends FrameLayout /* or something else you want */ {
    final View mParent;
    final TextView mTitle;

    public ItemView(Context context) {
        super(context);
        addView(mParent = inflate(context, R.layout.item_something, null));

        mTitle = (TextView) mParent.findViewById(R.id.title);
    }

    public void setItem(Object/*Item type*/ item) {
        mTitle.setText(item.getTitle());
        /* do anything you want here */
    }
}

Adapter getView:

final ItemView itemView;
if(convertView != null && convertView instanceOf ItemView) {
    itemView = (ItemView) convertView;
} else {
    itemView = new ItemView(context);
}
itemView.setItem((ItemType) getItem(position));
return itemView;
Hossain Khademian
  • 1,616
  • 3
  • 19
  • 29
  • I will check your solution but still: can you explain me what is wrong with mine? – Malvinka Nov 10 '14 at 22:27
  • 1
    use `ItemView` and then in `onListItemClick` simply cast `v` or `view` (better named) to `ItemView`. you can do it in `onListItemSelectListener` because you use MultiChoice Option – Hossain Khademian Nov 11 '14 at 05:50