Android 自定义列表对话框

发布于 2024-10-20 20:34:47 字数 729 浏览 9 评论 0原文

你好,
我正在开发一个简单的文件浏览器应用程序。我已经设置了大部分内容(其中列出了不同目录中的所有内容以及不包含的内容),但是我现在所坚持的(花了几个小时)是当选择列表项时,我想出现一个自定义列表对话框。我在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 语句的几种不同变体,但我尝试的一切都失败了。

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();

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

第几種人 2024-10-27 20:34:47

您收到的项目整数是包含您的操作的 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
}

但首选第一种方法

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

Or you could do like this:

if (items[item].equals("Info"))
{
     // Info item
}
else if (items[item].equals("Rename")
{
     // Rename, and so one
}

But the first method is prefered

苏大泽ㄣ 2024-10-27 20:34:47

有点晚了,但这可能会有所帮助。
我用它来填充对话框中的自定义列表。
我使用的是光标,但您也可以使用 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();

A little late but this may help.
I'm using it to populate a custom list in a dialog.
I'm using a cursor but you can also use a ArrayAdapter or whatever suits your fancy:

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();
哑剧 2024-10-27 20:34:47
  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();
  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();
删除→记忆 2024-10-27 20:34:47

并调用您想要打开单个选择对话框的对话框。
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");

and call in that dialog which u want to open a single select dialog..
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");
醉南桥 2024-10-27 20:34:47

我在对话框中调用对话框,这是我的代码。

试试这个:

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;
       }
    }

I am calling Dialog box in Dialog here is my code..

Try This :

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;
       }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文