16

こんにちは、
私は単純なファイル ブラウザー アプリに取り組んでいます。私はそれのほとんどをセットアップしました(さまざまなディレクトリのすべてをリストし、そうでないものをリストします)が、私が今行き詰まっているのは(数時間作業しました)、リスト項目が選択されたときです。カスタム リスト ダイアログが表示されます。Android開発ページでこのコードを見つけて、少し改造しました。現在、選択されたもののトーストを表示するだけですが、3 つのアイテムを別々にする必要があります。つまり、乾杯以上のことを行い、選択ごとに異なるコマンドを実行したいと考えています。これが私の現在のコードです

    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

受け取るアイテムの整数は、アクションを含む 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
}

ただし、最初の方法が優先されます

于 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

単一の選択ダイアログを開きたいダイアログを呼び出します. 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 に答える
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 に答える
-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 に答える