来自edittext android的微调器

时间:2014-11-24 14:26:44

标签: android dialog android-edittext spinner

我的情景:

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

这就是我的意思:

enter image description here

这就是我所做的:

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

}

1 个答案:

答案 0 :(得分:0)

尝试更新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;
        }

    }