简体   繁体   中英

Spinner from editext android

My Scenario:

When I click the top (+)icon there is a dialog displayed with editext and If I enter some text and click ok button the text should be added to my spinner which I am unable to do it.

Here is what I mean to say:

在此处输入图片说明

This is what I have done:

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

}

Try to update the adapter outside the 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();
}

EDIT : Here's how to implement your custom adapter (I made it private so it'd use the same dataList. Therefore, you don't need to call any updateData() function, just to notify the adapter that the data has changed with 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;
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM