Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android custom list dialog

Hi,
I'm working on a simple file browser app. I have most of it set up (where it lists everything out in the different directories and what not) but what I'm stuck on right now (worked on it for a few hours) is when a list item is selected, I want to have a custom list dialog appear. I found this code on the android development page and modded it slightly. Currently it just gives a toast of what was selected but I need the three items to be separate. That is, I'd like to do more than a toast and have each selection run different commands. Here is my current code

    final CharSequence[] items = {"Info", "Rename", "Delete"};

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Options for " + file.getName());
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
        }
    }).show();

Thanks to anyone who can help me just separate it. I've tried a few different variations of if statements and what not but everything I've tried has failed.

like image 777
user577732 Avatar asked Mar 09 '11 18:03

user577732


People also ask

How can I display a list view in an android alert dialog?

Navigate to the app > res > layout and create a new layout file. Add a ListView as shown below. This layout would be displayed inside the AlertDialog.

What is custom dialog in android?

The custom dialog uses DIALOG to create custom alert in android studio. Dialog display a small window i.e a popup which draws the user attention over the activity before they continue moving forward. The dialog appears over the current window and display the content defined in it.

How do I show a list in alerts?

Android App Development for Beginners This example demonstrates how do I display a listView in an android aler dialog. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How many types of dialogs are there in android?

There are three kinds of lists available with the AlertDialog APIs: A traditional single-choice list. A persistent single-choice list (radio buttons) A persistent multiple-choice list (checkboxes)


2 Answers

The item integer you receive is the index of the charsequence array that contains your actions, so to get the action that was selected you could do like this (inside your onClick method):

if (item == 0)
{
     // Info item
}
else if (item == 1)
{
     // Rename, and so one

Or you could do like this:

if (items[item].equals("Info"))
{
     // Info item
}
else if (items[item].equals("Rename")
{
     // Rename, and so one
}

But the first method is prefered

like image 193
Durza007 Avatar answered Sep 22 '22 07:09

Durza007


A little late but this may help. I'm using it to populate a custom list in a dialog. I'm using a cursor but you can also use a ArrayAdapter or whatever suits your fancy:

Dialog aDialog = new Dialog(this);
AlertDialog.Builder bDialog = new AlertDialog.Builder(this);

Cursor books = managedQuery(booksprovider.CONTENT_URI_BOOKS, null, null, null, null);
ListView booksToAdd = new ListView(this);
SimpleCursorAdapter books_list = new SimpleCursorAdapter(this, R.layout.shelves_add, books, 
    new String[] { BOOKS_TITLE, BOOKS_AUTHOR },//columns to include in view 
    new int[] { R.id.search_results_title,  R.id.search_results_author } );//views to bind columns to

booksToAdd.setAdapter(books_list); 
bDialog.setView(booksToAdd);

bDialog.setPositiveButton("Add to Shelf", new DialogInterface.OnClickListener() { });
aDialog = bDialog.create();
like image 45
Rickey Avatar answered Sep 20 '22 07:09

Rickey