我正在开发一个简单的文件浏览器应用程序.我有大部分设置(它在不同的目录中列出了一切,而且没有),但我现在被困在一起(在几个小时内工作了几个小时)是选择列表项目时,我想要出现一个自定义列表对话框.我在android开发页面上找到了这段代码并稍微修改了一下.目前它只是对所选内容进行了祝酒,但我需要将这三个项目分开.也就是说,我想做的不仅仅是祝酒,让每个选择运行不同的命令.这是我当前的代码
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.
您收到的项目整数是包含您的操作的字符序列数组的索引,因此要获取选择的操作,您可以这样做(在您的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
或者你可以这样做:
if (items[item].equals("Info"))
{
// Info item
}
else if (items[item].equals("Rename")
{
// Rename, and so one
}
但首选第一种方法
这篇关于Android 自定义列表对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!