問題描述
我正在開發一個簡單的文件瀏覽器應用程序.我有大部分設置(它在不同的目錄中列出了一切,而且沒有),但我現在被困在一起(在幾個小時內工作了幾個小時)是選擇列表項目時,我想要出現一個自定義列表對話框.我在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 自定義列表對話框的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!