views:

335

answers:

2

I have a ListView in my extended ListActivity class.

And then I have the following code to detect my click on the list

protected void onListItemClick(ListView l, View v, int position, long id)
{
   Log.i("", ""+l.getSelectedItem().toString());
}

I want to get the data string associated to my click.

  1. When I run the code, I get an error with getSelectedItem().

http://developer.android.com/reference/android/widget/ListView.html. getSelectedItem() is not method of ListView but it is defined in the superclass AdapterView. How is that possible? Rolling Eyes

2. Also , I'm not sure I understand this line: android.widget.AdapterView when it also inherits from android.view.ViewGroup isn't that multiple inheritance?

+1  A: 
  1. You can get a Java stack trace via adb logcat, DDMS, or the DDMS perspective in Eclipse, which will help you pin down your error.

  2. That demonstrates an inheritance tree, which is supported by most modern OO languages. ViewGroup is the parent of AdapterView which is the parent of AbsListView which is the parent of ListView.

CommonsWare
+1  A: 
  1. the easiest way to do this is probably this:

    getListView().setOnItemClickListener(this); //in ListActivity.onCreate

then add this method:

public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
    getListAdapter().getItem(id); //you may need to cast this to whatever you expect
}
  1. you can take a look at the inheritance tree - there you can see that no multiple inheritance is involved in this case :)

    • java.lang.Object
    • android.view.View
    • android.view.ViewGroup
    • android.widget.AdapterView
    • android.widget.AbsListView
    • android.widget.ListView
sashomasho