简体   繁体   中英

How to set adapter for spinner in alert dialog

i´m making Android app and i found a problem. I have activity and i have to make alert dialog from it. That wokrs fine, but i need a spinner in that dialog too and i cant set up adapter properly. App crashes with NullPointer ex. Piece of Dialog xml with spinner:

    <Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:drawSelectorOnTop="true" />

And here is code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_list);
    MyListView = (ListView) findViewById(android.R.id.list);
    registerForContextMenu(MyListView);
    myList = getIntent().getParcelableArrayListExtra(
            MainActivity.NUMBER_LIST);
    for (MyCallLog m : myList) {
        stringList.add(m.getNumber());
    }
    MyListView.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, stringList) {
    });
}

And Dialog code:

public void DialogData(final int DialogTyp) {
    Context mContext = getApplicationContext();
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(LAYOUT_INFLATER_SERVICE);
    final View layout = inflater.inflate(R.layout.dialog, null);


    AlertDialog.Builder MyBuilder = new AlertDialog.Builder(this);

    MyBuilder.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    if (DialogTyp == 1) {

                    }

                    else if (DialogTyp == 2) {
                        stringList.remove(aktual);
                    }

                    MyListView.invalidateViews();
                    return;
                }
            });

    MyBuilder.setNegativeButton("Storno",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    return;
                }
            });

    if (DialogTyp != 2)
        MyBuilder.setView(layout);

    AlertDialog MyAlertDialog = MyBuilder.create();

    if (DialogTyp == 0)
        MyAlertDialog.setTitle("Nový záznam");
    else if (DialogTyp == 1) {
        addItemsOnSpinner();
        MyAlertDialog.setTitle("Editace záznamu");
        ((EditText)layout.findViewById(R.id.cislo)).setText(myCallLog.getNumber());
        ((EditText)layout.findViewById(R.id.delka)).setText(String.valueOf(myCallLog.getDuration()));
        ((EditText)layout.findViewById(R.id.datum)).setText("25.2.2015");
        ((Spinner)layout.findViewById(R.id.spinner)).setSelection(getSelectedOperator(myCallLog.getOperator()));
    } else if (DialogTyp == 2) {
        MyAlertDialog.setTitle("Smazání záznamu" + aktual);
        MyAlertDialog.setMessage("Opravdu chcete smazat vybraný záznam?");
    }

    MyAlertDialog.show();
}

public void editList(int pozition, String number, String date, String spinner){

}

public void addItemsOnSpinner() {


    List<String> list = new ArrayList<String>();
    list.add("O2");
    list.add("T_MOBILE");
    list.add("VODAFONE");
    spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_item, list);
        spinner.setAdapter(adapter);
  }

Here on the last line there should be error. Where is the problem or what i have to do to set up it correctly?

spinner is null because you're trying to findViewById from your Activity, which is not the dialog layout. Instead pass the spinner you have retrieved here

((Spinner)layout.findViewById(R.id.spinner))

into your public void addItemsOnSpinner() {...} method.

You are trying to get the Spinner from a different view, if the Spinner is inside the dialog then you should do:

AlertDialog MyAlertDialog = MyBuilder.create();
Spinner spinnerInsideDialog = (Spinner) MyAlertDialog.findViewById(R.id.spinner);

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