0

I have made a listview. The plan is that when you select an item, it should appear selected (background color changes) and when you select another one, the one that was selected previously is normal again. Is there a way to do this? I've been trying a bunch of things and nothing works...

This is my code so far...

/*Listview testing*/
    final ListView listview = (ListView) findViewById(R.id.listView1);
    String[] values = new String[] { 
            "Case White", 
            "Operation Weser-Exercise", 
            "Case Yellow", 
            "April War", 
            "Operation Barbarossa", 
            "D-day" }; 

    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < values.length; ++i) {
      list.add(values[i]);
    }
    final StableArrayAdapter adapter = new StableArrayAdapter(this,
        android.R.layout.simple_list_item_1, list);
    listview.setAdapter(adapter);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, final View view,
          int position, long id) {
          //This doesn't work:            
          //listview.findViewById((int) selid).setBackgroundColor(Color.TRANSPARENT);
          view.setSelected(true);
          view.setBackgroundColor(Color.LTGRAY);
          Context context = getApplicationContext();

          CharSequence text = "id: " + id;
          int duration = Toast.LENGTH_SHORT;

          Toast toast = Toast.makeText(context, text, duration);
          toast.show();
          selid = id;
         }
    });

Marking one of them works, but then removing the selection is where I'm stuck. Any suggestions?

EDIT: what I'm looking for is for it to stay selected until I select another item

Marshall
  • 1,353
  • 3
  • 17
  • 38

2 Answers2

0

I think the problem is that your onItemClick method only fires for the list item that gets clicked. Every other item in the list remains unchanged.

What you need is a way to update all the items at once. The easiest way to do this is to write a custom adapter that extends BaseAdapter, then call myAdapter.notifyDataSetChanged(). I can provide an example if you'd like, but I would recommend looking for a tutorial on extending BaseAdapter.

Greg
  • 733
  • 6
  • 14
0

First thing is to configure the ListView to singleChoice because only one item can be selected :

<ListView
       android:id="@android:id/list"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:choiceMode="singleChoice">
</ListView>

Then, you need to create a selector. This is where you will configure colors for each defined state. The selected file is in res/drawable directory.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
   android:exitFadeDuration="@android:integer/config_mediumAnimTime">

   <item android:drawable="@android:color/holo_orange_dark" android:state_pressed="true"/>
   <item android:drawable="@android:color/holo_green_light" android:state_selected="true"/>
   <item android:drawable="@android:color/holo_green_light" android:state_activated="true"/>

</selector>

Then, on the item layout, add the attribute activatedBackgroundIndicator at top level:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:background="?android:attr/activatedBackgroundIndicator">

    <!-- your item content-->
</LinearLayout>

Finally, you need to link the selector with your ListView. This can be done in method onCreate of a ListActivity or in method onActivityCreated of a ListFragment.

this.getListView().setSelector(R.drawable.your_selector);

Thats all.

//EDIT

I didnot explain how to change the blue color. Here is the solution :

Create a file *res/drawable/listitem_background.xml* with the following content :

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

Replace the *@color/purple_dark* with the color of your choice.

Then, in your theme, add the following line :

<item name="android:activatedBackgroundIndicator">@drawable/listitem_background</item>

video: http://www.youtube.com/watch?v=BlZmE6Fk40M

Matej Špilár
  • 2,617
  • 3
  • 15
  • 28