0

As the title say.. Are the ListView equipped of some system to enable clicked item to stay selected ?

Thanks.

Rushino
  • 9,415
  • 16
  • 53
  • 93

3 Answers3

1

Besides, focus there is no such thing. You can improvise it by setting color to the item.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
0

Unfortunately not.

Here is an answer previously provided: Highlight ListView selected row

Because of the way the Android Framework is designed with Touch Mode, there is no way to do so without creating a custom ListView and programatically setting the background to a different color. You can keep track of your "last selected view" either through an id, a position, or a reference to the view itself.

Another option is to do this in a custom adapter's bind view, although this is more of a hassle because all your views have to be rebuilt in order for it to work properly.

I have read somewhere that they are to fix this issue in the Honeycomb though.

Community
  • 1
  • 1
Ken
  • 1,498
  • 2
  • 12
  • 19
  • see comment posted in Nikola answer. – Rushino Jul 28 '11 at 00:12
  • Yes you can set. No need of keeping track just tests can do the job. – Nikola Despotoski Jul 28 '11 at 00:16
  • well you may want to keep track for orientation change purposes – Ken Jul 28 '11 at 00:16
  • Like Nikola said getSelectedItem doesn't get you the view itself, just information about the object enclosed in the view/adapter You are able to use OnItemClick to do what you want; you can have a variable "lastSelectedView" which keeps track of the highlighted view, and everytime the OnItemClick is called, you can set the background of the lastSelectedView to your default background, and then set the background of your newly clicked item to the color of your choice – Ken Jul 28 '11 at 00:24
0

Sorry guys but i found a way to do so and it work fine. It was exactly what i was looking for. To help others i will post an exemple.

  1. First, you have to implement the mode in the layout with android:choiceMode="singleChoice".

  2. You need to specify a multi-choice list in the adapter.

ArrayAdapter adapter = new ArrayAdapter(monContext, android.R.layout.simple_list_item_single_choice, maListePrenoms);

3.Then in the code you can get the value of the selected item...

btnAfficherNom.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                Toast.makeText(monContext, lstPersonnes.getItemAtPosition(lstPersonnes.getCheckedItemPosition()).toString(), Toast.LENGTH_SHORT).show();
            }
        });
Rushino
  • 9,415
  • 16
  • 53
  • 93