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.