2011-03-09 104 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");