2011-03-09 106 views
16

嗨,
我正在研究一個簡單的文件瀏覽器應用程序。我已經設置了大部分內容(它列出了不同目錄中的所有內容,不包括哪些內容),但是我現在卡住的內容(處理它幾個小時)是在選擇列表項時,我想出現一個自定義列表對話框。我在android開發頁面上找到了這個代碼,並稍微修改了它。目前,它只是爲選擇什麼而敬酒,但我需要將三個項目分開。也就是說,我想做的不僅僅是敬酒,而且每個選擇都會運行不同的命令。這裏是我目前的代碼Android自定義列表對話框

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語句的幾種不同變體,但是我嘗試過的一切都失敗了。

回答

11

您收到的包含您的行動的CharSequence數組的索引,所以要獲取被選中,你可以做這樣的動作(你的onClick方法內部)的項目整數

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 
} 

但第一種方法是首選

+0

由於一噸,我非常接近我使用看起來就像第二個,而是像「信息」和「重命名」我有0和1個笑,所以我不得不像二者的混合代碼無論如何感謝一噸完美 – user577732 2011-03-09 19:22:25

1

晚了一點,但是這可能會有所幫助。 我正在使用它在對話框中填充自定義列表。 我使用遊標,但你也可以使用一個ArrayAdapter或任何適合你的想象:

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(); 
0
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       String[] name = new String[] {"item1","item2"}; 
     builder.setItems(name, new OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       switch(which){ 
           case 0: 
        //click item 1 
      break; 
     case 1: 
//click item 2 
break; 
            } 

      } 
     }); 
     builder.show(); 
-1

我打電話對話框對話框這裏是我的代碼..

試試這個:

public class AddTimerDialog extends DialogFragment { 

    AlertPositiveListener alertPositiveListener; 

    interface AlertPositiveListener { 
     public void onPositiveClick(int position); 
    } 

    public void setPositiveClickListener(
      AlertPositiveListener alertPositiveListener) { 
     this.alertPositiveListener = alertPositiveListener; 
    } 

    OnClickListener positiveListener = new OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      AlertDialog alert = (AlertDialog) dialog; 
      int position = alert.getListView().getCheckedItemPosition(); 
      if (alertPositiveListener != null) 
       alertPositiveListener.onPositiveClick(position); 
     } 
    }; 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     Bundle bundle = getArguments(); 
     int position = bundle.getInt("position"); 

     AlertDialog.Builder b = new AlertDialog.Builder(getActivity()); 

     b.setSingleChoiceItems(ReminderSnooze.code, position, null); 

     b.setPositiveButton("OK", positiveListener); 

     AlertDialog d = b.create(); 

     return d; 
     } 
    } 
0

,並呼籲在該對話框其中U要打開一個選擇對話框.. FragmentManager經理= getFragmentManager();

 /** Instantiating the DialogFragment class */ 
     AddTimerDialog alert = new AddTimerDialog(); 
     alert.setPositiveClickListener(this); 

     /** Creating a bundle object to store the selected item's index */ 
     Bundle b = new Bundle(); 

     /** Storing the selected item's index in the bundle object */ 
     b.putInt("position", position); 

     /** Setting the bundle object to the dialog fragment object */ 
     alert.setArguments(b); 

     /** Creating the dialog fragment object, which will in turn open the alert dialog window */ 
     alert.show(manager, "alert_dialog_radio");