Amazon Gift Cards

Wednesday, January 14, 2009

Clicking Items in ListActivity

From our Display Contact Names in Android and Displaying AlertDialog in Android, lets further expand that so that we would make use if the items in our ListActivity when user click the an item list, this article would be a brief one. For the following code just add it to your class, you can view the full source code at the bottom if you don't know where to put them.
protected void onListItemClick(ListView l, View v, int position, long id) {
  alertDialog = new AlertDialog.Builder(this).create();
  alertDialog.setTitle("Item Selected");
  alertDialog.setMessage("You just clicked an 
  item position #" + String.valueOf(position));
  alertDialog.setButton("OK",new DialogInterface.OnClickListener(){
    public void onClick(DialogInterface dialog, int which) {
  } });;

  super.onListItemClick(l, v, position, id);

Quick Explanation
Here all we need to do it implement a function, onListItemClick, you can do this in eclipse by Right Click->Source->Override/Implement Method->onListItemClick.
There would be 4 arguments ListView l, View v, int position, long id, view this to see that they mean.

Display Contact Names in Android
Displaying AlertDialog in Android

Source of Clicking Items in ListActivity

Update History
   Jan 17, 2012 - Visual Update


emaborsa said...

Not complex, but it has helped me. Thanks

CheesyGoat said...

Thanks for this, helped a lot