Clicking Items in ListActivity

Public on January 14, 2009
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.
@Override
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) {
    return;
  } });
  alertDialog.show();

  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.

References
Display Contact Names in Android
Displaying AlertDialog in Android
android.app.ListActivity

Source
Main.java of Clicking Items in ListActivity


Update History
   Jan 17, 2012 - Visual Update




[Advertising]Faster Thinking - Speed up your brain


Faster Thinking Game



sentiment_satisfied Emoticon