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

StackOverflow https://stackoverflow.com/questions/17463696

  •  02-06-2022
  •  | 
  •  

سؤال

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;
}
هل كانت مفيدة؟

المحلول

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);
  }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top