簡體   English   中英

單擊“獲取警報”對話框項列表以顯示在其他片段,適配器或活動中

[英]Getting alert dialog item list to display in a different fragment, adapter or activity once clicked

我不知道如何執行此操作,我有一個列表,例如芒果,香蕉,菠蘿等(水果),單擊設置活動中的“選擇水果”部分后,我將使用警報對話框顯示它們。

我現在的問題是,一旦在不同的地方(例如,在類,片段和適配器上)選擇了它們,我需要顯示這些水果,我只是想知道如何獲取被單擊的項目並將其設置在我在項目中擁有的不同的textview上。在不同的活動中。

我的簡單對話框示例

 final String[] items = {"Apple", "Banana", "Orange", "Grapes","Mango","pawpaw", "ovacado", "peach", "pears",};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("List of Items")

                .setItems(items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), items[which] + " is clicked", Toast.LENGTH_SHORT).show();
                    }
                });
        builder.setPositiveButton("OK", null);
        builder.setNegativeButton("CANCEL", null);
        AlertDialog alertDialog = builder.create();

        alertDialog.show();

        Button button = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
       button.setBackgroundColor(Color.GREEN);
        button.setTextColor(Color.WHITE);

一般想法,我應該能夠獲得所選項目並將其顯示在適配器上。

holder1.fruitsTextView.setText( from the alert dialog selected item "mango" + fruitsDatas.get(position).getAllFruits());

我正在嘗試使用此示例的圖像。 那是我要在設置部分彈出的對話框,只是我要實現的示例

這是我的主要活動文本視圖,我要在其中顯示對話框中的選定項目

  private fun showAlertWithChoiceOption() {
        val builder = AlertDialog.Builder(this)
        builder.setTitle("Choose an Option")

        val picker = arrayOf("Camera", "File Explorer", "Cancel")
        builder.setItems(picker) { dialog, which ->
            when (which) {
                0 -> {
                   //Camera Choosen 
    dialog.cancel()}
                1 -> {
                    //File Explorer Choosen  
    dialog.cancel()              }
                2 -> {
//Cancel Choosen
                    dialog.cancel()
                }
            }
        }

        val dialog = builder.create()
        dialog.setCanceledOnTouchOutside(false)
        dialog.show()
    }

試試上面的代碼,它在Kotlin中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM