【问题标题】:How to change a value from AlertDialog?如何从 AlertDialog 更改值?
【发布时间】:2012-03-08 06:12:39
【问题描述】:

只要EditTextListView 很难弄清楚,我就尝试了另一种解决方案:ListViewTextViews 当你点击一个时,我会用听众捕捉到焦点TextView (父 ListView 上的 OnItemClickListener)然后我打开一个 AlertDialog

ISSUE :当我按下AlertDialog 上的OK 按钮时,我希望焦点TextView 获取警报对话框EditText 的文本值,但它不起作用,他保持相同的文本值。活动中:

        listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int pos, long id){

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

            id ++;
            t = new TextView(activity);
            t = (TextView) v;

            AlertDialog.Builder alert = new AlertDialog.Builder(activity);

            alert.setTitle("Title");
            alert.setMessage("Message");

            // Set an EditText view to get user input 
            final EditText input = new EditText(activity);
            input.setText(t.getText());
            alert.setView(input);

            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    //Changer la valeur dans la base et dans la liste


                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS,0);

                    t.setText(input.getText());

                    System.out.println("input avant : " + t.getText());
                }
            });
            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                }
            });


            alert.show();
        }
    });

我认为这段代码足以让您知道我的代码有什么问题。只是想让你知道:

  • ListViewt (TextView) 在活动类上声明为私有变量
  • 有一个Adapter 类可以自动构建TextView 列表
  • 如果我在ListView 侦听器上更改EditText 的值,它可以工作,但在对话框警报侦听器中,它不会。

询问您是否需要更多信息。

【问题讨论】:

    标签: android android-listview textview android-alertdialog


    【解决方案1】:

    我不知道您是如何构建适配器的,所以我猜一个答案。您必须将文本放入适配器数据中(例如,在您提供给适配器的 ArrayList 中)并在适配器上调用 notifyDataSetChanged()

    //ArrayList field on which your adapter is based ?!?(if you used this)
    private ArrayList<String> items;
    
     listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, final View v, int pos, long id){
    
                AlertDialog.Builder alert = new AlertDialog.Builder(activity);
    
                alert.setTitle("Title");
                alert.setMessage("Message");
                final EditText input = new EditText(activity);
                input.setText(t.getText());
                alert.setView(input);
    
                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        items.set(pos, input.getText().toString());
                        adapterObject.notifyDataSetChanged();  // the adapter you set in the listView.setAdapter();
                    }
                });
                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Canceled.
                    }
                });
    
    
                alert.show();
            }
        });
    

    【讨论】:

    • 天哪,你的权利。就像我做了一个自定义适配器但不知道如何使用它。无论如何,我从安卓开始。谢谢!
    • @SebastienFERRAND 如果您想要的只是一行带有TextView 的行,那么您最好使用简单的ArrayAdapter&lt;String&gt;。自定义适配器适用于您想要自定义行布局或具有更复杂数据结构的情况。
    • 是的,你是对的,谢谢你的建议,但我需要它在未来变得更复杂
    猜你喜欢
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多