When you click on the item ListView I change the color of the item

 public void onItemClick(AdapterView<?> parent, View view, int position, longid) { list.getChildAt(position).setBackgroundColor(Color.YELLOW); } 

After calling the method


Color becomes another item! How to make a normal color change?

    2 answers 2

    SO: Problems with Gallery.getChildAt (int position) .

    The point is that position is not a position in the adapter, but a position of the current View relative to the first visible View in the parent ViewGroup. Those. so it needs to work somehow like this list.getChildAt(list.getFirstVisiblePosition()+position)...

    In general, it is more logical to work with id, IMHO, although it may be no faster ...

    • It did not help, getFirstVisiblePosition () is always 0. I click on the item, it turns yellow (fine), I switch to another activation, I come back, the onResume in it is triggered notifyDataSetChanged () and the opposite is true, right from the end! If you notifyDataSetChanged (), it works! Did: view.setBackgroundColor (Color.YELLOW); problem lagged! - katso
    • one
      And, there was such a problem. Checkboxes tried to put in his custom sheet. The point is that the view is not re-created during the second show of the activity, but is being used again and their positions are distorted, or rather they go in reverse order. It’s right not to use the position, but to change the values ​​in the adapter, then, using GetView, create a display based on the data. [Here] [1] marked the correct answer, the second paragraph (the first we have already discussed :). [1]: stackoverflow.com/questions/5850456/… - Yura Ivanov
    • Thanks, it turned out! - katso

    But in my opinion it is easier to change the color of the adapter.

    • Sobsno I did just that - katso