androidlistviewcustom-lists

Android custom listview - change background and text color of selected item is not working?


I am making custom listview with different background and text color. I have overridden setSelected(int position) method from my custom adaptor. its is working Fine in 7 inch tabs but not working in Samsung Galaxy Note Tablet (10.1 inch).

if(selectedPosition == position){
          ((TextView)convertView.findViewById(R.id.folderName)).setTextColor(Color.WHITE);
          ((LinearLayout)convertView.findViewById(R.id.folderLayout)).setBackgroundResource(R.drawable.folders_list_bg_s);
      }else{
          ((TextView)convertView.findViewById(R.id.folderName)).setTextColor(Color.BLACK); 
          ((LinearLayout)convertView.findViewById(R.id.folderLayout)).setBackgroundResource(R.drawable.folders_list_bg);
      }

And the setselection method as

public void setSelected(int position) {
    selectedPosition = position;
}

Solution

  • After modifying the adapter we need to notify with notifyDatasetChanged() like

    if(selectedPosition == position){
          ((TextView)convertView.findViewById(R.id.folderName)).setTextColor(Color.WHITE);
          ((LinearLayout)convertView.findViewById(R.id.folderLayout)).setBackgroundResource(R.drawable.folders_list_bg_s);
          notifyDatasetChanged();
      }else{
          ((TextView)convertView.findViewById(R.id.folderName)).setTextColor(Color.BLACK); 
          ((LinearLayout)convertView.findViewById(R.id.folderLayout)).setBackgroundResource(R.drawable.folders_list_bg);
      }