views:

59

answers:

1

I would like to open a custom dialog when someone clicks on a listview entry. That dialog will need to know the text that was clicked on in order to display additional information on that particular entry. Can anyone point me in the right direction on how to accomplish that? Thanks!

+1  A: 

On the ListActivity, override the onListItemClick method. There, you will get the position of the item that was clicked. As you said you want to know the text that is on the item that was clicked, I suppose you have a simple List. In that case, I guess you have, for instance, an array with strings to populate the list.

public void onListItemClick(ListView parent, View v, int position,
                              long id) {
  String itemText = items[position]);
}

So, in this case I'm supposing you have an array of Strings called items. The next step would be to create a Dialog, which can be done this way:

public void onListItemClick(ListView parent, View v, int position,
                              long id) {
    String itemText = items[position]);

    new AlertDialog.Builder(this)
        .setTitle("Title for " + itemText)
        .setMessage("Custom message for "+itemText)
        .setNeutralButton("Close", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dlg, int sumthin) {
            // do whatever you want to do
        }
    }).show();
}

By the way... if you want to receive nice answers here, make sure you provide nice questions. By "nice question" I mean something with a little bit of your code, so that we can get a better idea of how to help you ;)

Cristian
Perfect, thanks!
JonF