0

I am trying to make a simple interface that allows the user to select a single item in a list by tapping and then press a button to remove it. I have the choice mode on the list set to singleChoice but whenever I tap a item it highlights for a brief moment the goes back to normal. Whenever I press the remove button the getSelectedItem and getSelected View return null. Here is the ListView code:

<ListView android:id="@+id/times"
    android:choiceMode="singleChoice"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/time"
    android:layout_above="@+id/every"/>

This is the method called on remove, where list is the ListView and adapt is its ArrayAdapter, using an ArrayList.

public void remove(View view)
{
    System.out.println(list.getSelectedItem()+"\n"+list.getSelectedView());
    adapt.remove((String)list.getSelectedItem());
}

Edit: I am trying to make this app's minimum API level 10 in case that helps.

yesennes
  • 1,147
  • 1
  • 10
  • 19

2 Answers2

2

List items are not selectable out of the box by design. You can find more about that here:

Imagine a simple application, ApiDemos for example, that shows a list of text items. The user can freely navigate through the list using the trackball and they can also scroll and fling the list using their finger. The issue in this scenario is the selection. If I select an item at the top of the list and then fling the list towards the bottom, what should happen to the selection? Should it remain on the item and scroll off the screen? In this case, what would happen if I then decide to move the selection with the trackball? Or worse, if I press the trackball to act upon the currently selected item, which is not shown on screen anymore. After careful considerations, we decided to remove the selection altogether.

To make them selectable, there are 2 options:

  1. Use the row item layout android.R.layout.simple_list_item_single_choice, which renders radio button for each row. With ArrayAdapter, you can use this layout as:

    listView.setAdapter(new ArrayAdapter<>(this,
                        android.R.layout.simple_list_item_single_choice,
                        dataArray));
    
  2. Use selectors as background. You can refer This post to know how to do that - how to set choice mode single for listview with images

Community
  • 1
  • 1
Ganesh Kumar
  • 3,220
  • 1
  • 19
  • 27
1

These are two issues:

  1. "Selected Highlighting" - for this you need to create depending on how you want to highlight the item a drawlable/color selector - with state_activated="true"

example drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:state_pressed="true" android:drawable="@color/orange" />
   <item android:state_activated="true" android:drawable="@color/green" />
   <item android:drawable="@android:color/transparent" />
</selector>
  1. Selection in ListView in any choice mode except NONE is actually handled in checked state so to get the current selection you need to getCheckedItemPosition: http://developer.android.com/reference/android/widget/AbsListView.html#getCheckedItemPosition()
Raanan
  • 4,777
  • 27
  • 47