1

I have implemented a list view using a custom array adapter. Now I want to get the selected item of the list view. I know that there are solutions using onclick listeners. However I would like to use the getSelectedItem() method of the ListView (AdapterView) class. The method always returns null. The other getSelected* methods also do not work.

// onCreate
    mList = (ListView) findViewById(R.id.listView);

// set in Broadcast Receiver (inner class)
    mList.setAdapter(new ListAdapter(getApplicationContext(),
                R.layout.list_view_item, R.id.textView,
                itemList));

// onButtonClick
    Log.i(TAG, "Selected: " + mList.getSelectedItem());

OnButtonClick is a callback of a seperate button. If I click on it the selected item should be printed in logcat but it returns everytime null. Can anyone help me?

XML:

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@android:color/background_light"
    android:drawSelectorOnTop="false"
    android:listSelector="@android:color/holo_blue_light" >
</ListView>

<Button
    android:id="@+id/buttonContinue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/listView"
    android:layout_centerHorizontal="true"
    android:onClick="onButtonClickContinue"
    android:text="Continue" />
</RelativeLayout>

If I select an item the color changes.

code monkey
  • 2,094
  • 3
  • 23
  • 26
  • Please post your XML i think i didn't declare your items in your xml – Boldbayar Oct 16 '14 at 08:42
  • Can you tell us a little more about your application? At which situation do you want to get the selected item? – Kody Oct 16 '14 at 08:44
  • @user2572585 my bad for the misunderstanding, sorry: I removed my solution as it clearly didn't match your question. – Rick77 Oct 16 '14 at 09:19
  • 1
    @Rick77 No problem ;) You wanted to help. So thanks. – code monkey Oct 16 '14 at 09:28
  • (*very*) dumb question: are you working in the UI thread all the time? – Rick77 Oct 16 '14 at 09:43
  • also, does a getItemAtPosition(0) work (trying to get my bearings)? – Rick77 Oct 16 '14 at 09:48
  • Yes I only use the UI thread. Could that be a problem? getItemAtPosition(0) works fine – code monkey Oct 16 '14 at 09:48
  • no, I sort of hoped you did use threads, because it could have provided a line of investigation. As for the getItemAtPosition, I have read this answer to a similar problem: http://stackoverflow.com/a/13627960/2177061 (but I must be honest, it sound silly to me and I don't know if it's correct... Still checking, though) – Rick77 Oct 16 '14 at 09:51
  • also, just to be sure, could you try again after adding in the ListView's XML android:choiceMode="singleChoice" and see if something changes? – Rick77 Oct 16 '14 at 09:54
  • No it doesn't change anything. – code monkey Oct 16 '14 at 10:01
  • last ditch effort (then, I'm completely clueless... sorry): have you tried passing the actual activity context to the Adapter instead of the one in the app? Something like new ListAdapter(MyActivityName.this, R.layout.list_view_item, R.id.textView, itemList)? – Rick77 Oct 16 '14 at 10:14
  • 1
    No unfortunately it didn't work. However thanks for your effort! I think I will use the solution with the listener because it looks like this is getting really time-consuming... – code monkey Oct 16 '14 at 10:45

3 Answers3

2

Implement you class

public class xyz extends Activity implements OnItemSelectedListener

then give onitemclicklistener of that activity

mList.setOnItemSelectedListener(this);

And now implement the onItemselected method

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    // u will get the position and selected item here
}
Arpit Patel
  • 1,561
  • 13
  • 23
2

I check the getSelectedItem source code

public Object getSelectedItem() {
    T adapter = getAdapter();
    int selection = getSelectedItemPosition();
    if (adapter != null && adapter.getCount() > 0 && selection >= 0) {
        return adapter.getItem(selection);
    } else {
        return null;
    }
}

maybe you should check your the getItem method in the ListAdapter

Kaushik
  • 6,150
  • 5
  • 39
  • 54
chinaanihchen
  • 396
  • 2
  • 5
  • chinaanihchen's answer is actuall interesting: what is the Log.d of the getAdapter, getCount, before the getSelectedItem? – Rick77 Oct 16 '14 at 09:21
  • Yes the answer is interesting. However the custom adapter is not the problem. It seems everything OK. I also tried it with an ArrayAdapter and I get the same problem. – code monkey Oct 16 '14 at 09:40
2

I figured out that the OnItemSelected method is never/rarely called on touch devices. It fires up if you use the emulator cross-navigation and on special devices with hybrid or non-touch control. It is only called if you scroll through the list but not if you click on it.

Thats why you should use the OnItemClickListener on a general tablet/smartphone with touch control.

code monkey
  • 2,094
  • 3
  • 23
  • 26
  • This blog post on [touch mode](http://android-developers.blogspot.com/2008/12/touch-mode.html) has more background. – Edward Brey Aug 09 '16 at 22:55