16

嗨,
我正在开发一个简单的文件浏览器应用程序。我有大部分设置(它在不同的目录中列出了一切,而且没有),但我现在被困在一起(在几个小时内工作了几个小时)是选择列表项目时,我想要出现一个自定义列表对话框。我在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 语句变体,但我尝试过的一切都失败了。

4

5 回答 5

11

您收到的项目整数是包含您的操作的字符序列数组的索引,因此要获取选择的操作,您可以这样做(在您的 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
}

但首选第一种方法

于 2011-03-09T19:10:20.753 回答
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();
于 2012-02-14T04:35:39.997 回答
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();
于 2013-02-24T12:31:07.163 回答
0

并调用您想要打开单个选择对话框的对话框。 FragmentManager manager = 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");
于 2014-06-26T09:34:43.983 回答
-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;
       }
    }
于 2014-06-26T09:26:22.037 回答