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