【问题标题】:Spinner from editext android来自edittext android的微调器
【发布时间】:2014-11-24 22:57:47
【问题描述】:

我的场景:

当我单击顶部 (+) 图标时,会显示一个带有 editext 的对话框,如果我输入一些文本并单击确定按钮,文本应该添加到我无法执行的微调器中。

这就是我的意思:

这就是我所做的:

protected void showInputDialog() {

    // get prompts.xml view
    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
    View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            MainActivity.this);
    alertDialogBuilder.setView(promptView);


    // setup a dialog window
    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // Spinner element
                    listsp = (Spinner) findViewById(R.id.listspinner);

                    listtext = (EditText) findViewById(R.id.list_text);
                    list = new ArrayList<String>();
                    list.add(listtext.getText().toString());
                    listadapter = new ArrayAdapter<String>(getApplicationContext(),
                            android.R.layout.simple_spinner_item, list);
                    listadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    listsp.setAdapter(adapter);
                }
            })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

    // create an alert dialog
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();

}

【问题讨论】:

    标签: android dialog android-edittext spinner


    【解决方案1】:

    尝试在 onClick 之外更新适配器:

    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      //Whatever else
      listsp = (Spinner) findViewById(R.id.listspinned);
      list = new ArrayList<String>();
      listadapter = new MyArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, list);
      listadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      listsp.setAdapter(adapter);
    

    }

    protected void showInputDialog() {
    
    // get prompts.xml view
    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
    View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            MainActivity.this);
    alertDialogBuilder.setView(promptView);
    
    
    // setup a dialog window
    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    listtext = (EditText) findViewById(R.id.list_text);
                    updateAdapter(listtext.getText().toString());
                }
            })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
    
    // create an alert dialog
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
    
    }
    
    protected void updateAdapter(String input) {
      list.add(input);
      listadapter.notifyDataSetChanged();
    }
    

    编辑:这是实现自定义适配器的方法(我将其设为私有,因此它会使用相同的 dataList。因此,您不需要调用任何 updateData() 函数,只需通知适配器数据有更改为 notifyDataSetChanged()) :

    private class MyArrayAdapter extends BaseAdapter implements SpinnerAdapter {
    
            @Override
            public int getCount() {
                return list.size();
            }
    
            @Override
            public Object getItem(int position) {
                return list.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            public View getView(int position, View view, ViewGroup parent) {
                TextView text = new TextView(lexs);
                text.setText(list.get(position).getName());
                return text;
            }
    
        }
    

    【讨论】:

    • 感谢 simon 的回复..我能知道如何为此添加自己的适配器..你能告诉我怎么做吗?
    猜你喜欢
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    相关资源
    最近更新 更多