Android的自定义列表对话框 [英] Android custom list dialog

查看:471
本文介绍了Android的自定义列表对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我工作的一个简单的文件浏览器应用程序。我有大部分设置(如果它列出了所有的东西在不同的目录,什么不可以),但是当一个列表项被选中了我,现在我被困在(工作就可以了几个小时)是的,我想有一个自定义列表对话框出现。我发现Android开发页面上此code和改装稍微。目前,它只是给了什么选择敬酒,但我需要这三个项目是分开的。也就是说,我想比面包做多,并有各自的选择运行不同的命令。这是我目前的code

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();

由于任何人谁可以帮助我只是分开吧。我试过的if语句一些不同的变化并没有什么,但一切我已经试过失败。

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.

推荐答案

该项目整数收到是包含行动的CharSequence数组的索引,因此要获得被选择,你可以做这样的动作(在你的OnClick方法):

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
}

不过,第一种方法是prefered

But the first method is prefered

这篇关于Android的自定义列表对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆