You must use your adapter to populate the list with elements. Then, in your activity or fragment, you must implement the onListItemClick event to handle a click on a list item.
EDIT: I am posting an example below where I use ListFragment - this is part of the code that I have now, I can post something specific to ListView later:
public class RecipiesActivity extends FragmentActivity { private RecipiesSummaryListAdapter m_listAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListFragment lf = (ListFragment) fm.findFragmentById(R.id.my_list_fragment); lf.setListAdapter(m_listAdapter); }
Then in ListFragment :
public class MyListFragment extends ListFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return (View) inflater.inflate(R.layout.myListViewXml, container, false); } @Override public void onListItemClick(ListView l, View v, int position, long id) {
Learn more about ListFragment here .
I think a very good example (although the extension of the ArrayAdapter instead of BaseAdapter is represented by https://stackoverflow.com/a/3168379/) .
One thing to keep in mind:
- The button should not focus (i.e. use XML
android:focusable="false" - as in your XML). - You need to capture the
onItemClick event as shown below (extract from the link above):
the code:
listview.setOnItemClickListener(new OnItemClickListener() { //@Override public void onItemClick(AdapterView arg0, View view, int position, long id) { // user clicked a list item, make it "selected" selectedAdapter.setSelectedPosition(position); //Do your stuff here }
Finally, pay attention to one point. This solution means that you click on the list bar and the event fires.
I think that if you want the user to click the button and the event to activate the button, you need to set the listview.setItemsCanFocus(true) call right after inflating the ListView XML file and make sure that the button is focusable in XML. Listening for the onClick event should work then.
This is described in slide 25 of the GoogleIO World of ListView presentation (you can get it here and video here )
Hope this helps