Hello. There is a custom array adapter. In getView() highlight the desired Item

 if (index == data[position]) convertView.setBackgroundColor(getContext().getResources().getColor(R.color.red)); else convertView.setBackgroundColor(getContext().getResources().getColor(android.R.color.transparent)); 

But with this approach, if I just click on an item, then selected does not work.

Decided to use setSelected()

 if (index == data[position]) convertView.setSelected(true); else convertView.setSelected(false); 

But item refuses to stand out. Stavil debugging messages with isSelected. So on the item you’ve received, it’s as if it was selected, but visually it’s not.

By event OnItemClickListener

 if (index == data[position]) convertView.setSelected(true); else convertView.setSelected(false); 

Everything stands out. Tell me what could be the problem? Why setSelected() in OnItemClickListener work, but in getView() works, but it doesn't stand out? How to solve it. It is necessary that by clicking on the item a selection flashed and item was highlighted.

getView

public View getView (int position, View convertView, ViewGroup parent) {ViewHolder holder;

  if (convertView == null) { convertView = this._layoutInflater.inflate(R.layout.items, parent, false); holder = new ViewHolder(); holder.name = (TextView)convertView.findViewById(R.id.name); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } if (index == data[position]) { convertView.setBackgroundColor(getContext().getResources().getColor(R.color.red)); //convertView.setSelected(true); } else convertView.setBackgroundColor(getContext().getResources().getColor(android.R.color.transparent)); //convertView.setSelected(false); holder.name.setText(data[position]); return convertView; } 
  • Please post the method code getView () - DevOma
  • If you need to lay out, but everything is simple there. I get markup widgets and this selection condition itself. In OnItemClickListener I update the record in the database and immediately try to highlight the pressed item. - Ivan
  • Lay out to clearly see what you mean. - DevOma
  • Added watch - Ivan

1 answer 1

Try creating a lvBg.xml selector for ListView in drawable .

 <?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@color/pressed_color"/> <item android:drawable="@color/default_color" /> </selector> 

in res / value / colors:

 <color name="pressed_color">#4d90fe</color> <color name="default_color">#ffffff</color> 

And apply the background to the ListView

 android:background="@drawable/lvBg" 

If it fails, try this method should help