2

I create a class RecordActivity derived from ListActivity class, and set the choice mode and selector for the ListView defined in the .xml. The default behavior is the selected item will be highlighted only when I pressed it. I want to keep the selected item to be highlighted. I tried to override the getView method for ArrayAdapter, however, it doesn't work. Any help will be appreciated.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

   <ListView  android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>     
</LinearLayout>

public class RecordsActivity extends ListActivity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.records);

      List<Record> values = getAllRecords();
      // Use the SimpleCursorAdapter to show the
      // elements in a ListView
      adapter = new ArrayAdapter<Record>(this, android.R.layout.simple_list_item_1, 
                                                            values);
      setListAdapter(adapter);    
      getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
      getListView().setSelector(android.R.color.holo_red_dark);
   }

   @Override
   protected void onListItemClick(ListView l, View v, int position, long id) {
      selectedItem = position;
      v.setSelected(true);      
   }
}
yinhao
  • 101
  • 1
  • 6
  • You can find Your way in these Links : [Link1][1] [Link2][2] [Link3][3] [Link4][4] [1]: http://stackoverflow.com/questions/5058291/highlight-listview-selected-row [2]: http://stackoverflow.com/questions/10788688/programmatically-select-item-listview [3]: http://stackoverflow.com/questions/5853719/highlighting-the-selected-item-in-the-listview-in-android [4]: http://stackoverflow.com/questions/5972155/does-anyone-know-how-to-highlight-a-selected-item-in-a-android-listview – Arash GM Apr 15 '13 at 07:48

3 Answers3

4

Try using simple_list_item_activated_1

adapter = new ArrayAdapter<Record>(this, android.R.layout.simple_list_item_activated_1, 
                                                        values);
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
1
    private int selectedValue;
    private View row;

  ListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            selectedValue=arg2;
            if(row!=null)
            {
                                row.setBackgroundResource(R.drawable.group_item_normal);
            }
            row=arg1;
        arg1.setBackgroundResource(R.drawable.group_item_pressed);

        }
    });

Do in getView():- v is object that return by getView()

  if(selectedValue==position)
        {
            v.setBackgroundResource(R.drawable.group_item_pressed);

        }
        else{
            v.setBackgroundResource(R.drawable.group_item_normal);

        }
user2251725
  • 406
  • 1
  • 5
  • 18
0

Try this, on honeycomb or greater, the selected item will be kept highlighted. Since android.R.layout.simple_list_item_activated_1 will only work on honeycomb or greater version, you should add the layout like this to support older platform.

int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1
                : android.R.layout.simple_list_item_1;


adapter = new ArrayAdapter<Record>(this, layout, values);
Homam
  • 5,018
  • 4
  • 36
  • 39