2

I have a CustomAdapter that works fine. The Adapter insert data into my Android GridView. Now i want when an item is checked the BackGround of the Item change to white, and when the other Item is selected, the previous selected Item color chage to GridView background, i mean just with SingleChoiceMode. I have wrote this :

 gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if(gridView.isItemChecked(position)) { view = gridView.getChildAt(position); view.setBackgroundColor(Color.WHITE); }else { view = gridView.getChildAt(position); view.setBackgroundColor(124333); //the color code is the background color of GridView } } }); 

But just the first If works, it means when i select a row, the color change to white, but when i select the other row, the previous is still white and the new selected is white too. What should i write in else statement?

1 Answer 1

3

You need to call adapter.notifyDataSetChanged(); every time you pressed on any item of your GridView for refresh GridView

 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { adapter.notifyDataSetChanged(); // call it here if(gridView.isItemChecked(position)) { view = gridView.getChildAt(position); view.setBackgroundColor(Color.WHITE); }else { view = gridView.getChildAt(position); view.setBackgroundColor(124333); //the color code is the background color of GridView } } 

If you just want to change the color of selected item in GridView, you can use android:listSelector for your GridView xml

<GridView ... android:listSelector="#ff0000" > </GridView> 
Sign up to request clarification or add additional context in comments.

1 Comment

Thx alot Dude. I used the XML one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.