1

I have a problem with my listview. What I want to do is when I click an item in the listview, it should remain highlighted, and when I click another item, new item should be highlighted.

Here's what I've done so far:

public void setSelection(int position) {
    if (selectedPos == position) {
        selectedPos = NOT_SELECTED;
    } else {
        selectedPos = position;
    }

    notifyDataSetChanged();
}

Adapter GetView:

if (position == selectedPos) {
  // your color for selected item
     viewHolder.tvTitle.setTextColor(context.getResources().getColor(R.color.main_blue));
     viewHolder.tvDetails.setTextColor(context.getResources().getColor(R.color.main_blue));
 }
else {
  // your color for non-selected item
     viewHolder.tvTitle.setTextColor(context.getResources().getColor(R.color.dark_grey));
     viewHolder.tvDetails.setTextColor(context.getResources().getColor(R.color.dark_grey));
    }

Activity:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        episodesAdapter.setSelected(position);
     }
 });

This is working as expected, but the problem is with notifyDataSetChanged, it's delaying the time to highlight item that is being selected next. What should I do to automatically get it highlighted? Any help would be truly appreciated. Thanks!

Update: I tested and it took 8-10secs before a new item is highlighted. There is a slight delay in highlighting an item.

  • have a look here for a possible solution -- http://stackoverflow.com/questions/16189651/android-listview-selected-item-stay-highlighted – Tasos Jan 20 '16 at 12:06
  • @Tasos thanks but what I want is to just highlight the textviews not the row –  Jan 20 '16 at 12:13
  • ok sorry. do you use an array for the listview? – Tasos Jan 20 '16 at 12:17
  • I have an arraylist (records/items) from server –  Jan 20 '16 at 12:19
  • ok so simply add to the array for all items (selected = false) and then when you click on the item set (selected = true) for that position in the array which is the same as the items position in the listview and change the colors. Lastly in your adapter you do a check ( if selected = true then change colors else do nothing) – Tasos Jan 20 '16 at 12:21
  • @Tasos can you post sample code? –  Jan 20 '16 at 12:49
  • Try this http://stackoverflow.com/questions/24630538/highlight-the-selected-item-in-horizontal-list-view/24630894#24630894 – Ketan Ahir Jan 20 '16 at 13:05
  • @KetanAhir I've done this but not exactly what I'm looking for –  Jan 21 '16 at 03:41

0 Answers0