Goal:
Display the value for the spinner in relation to AlertDialog
Problem:
What code am I missing in order for the spinner to be working with the value?
Info:
*I'm newbie in Android.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/alertFormElements2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="alertFormElements2"
android:text="Button" />
</LinearLayout>
form_elements2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="131dp" />
</RelativeLayout>
MainActivity
package com.jfdimarzio.t1;
import android.annotation.TargetApi;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void alertFormElements2(View vdf) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View formElementsView = inflater.inflate(R.layout.form_elements2,null, false);
// the alert dialog
new AlertDialog.Builder(MainActivity.this).setView(formElementsView)
.setTitle("Form Elements")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@TargetApi(11)
public void onClick(DialogInterface dialog, int id) {
String[] vvalue = new String[]{
"7",
"3",
"6",
"3",
"8",
"9",
"15",
"11",
"57"
};
Spinner _spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.spinner_item, vvalue);
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
_spinner.setAdapter(spinnerArrayAdapter);
dialog.cancel();
}
}).show();
} // alertFormElements
} // Class
答案 0 :(得分:0)
So you're not actually setting the spinner's values until the user clicks your ok button, at which point you immediately dismiss the dialog. You'll need to set the spinner up before then.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View formElementsView = inflater.inflate(R.layout.form_elements2,null, false);
String[] vvalue = new String[]{
"7",
"3",
"6",
"3",
"8",
"9",
"15",
"11",
"57"
};
// Set up spinner BEFORE launching dialog
final Spinner _spinner = formElementsView.findViewById(R.id.spinner);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.spinner_item, vvalue);
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
_spinner.setAdapter(spinnerArrayAdapter);
// the alert dialog
new AlertDialog.Builder(MainActivity.this).setView(formElementsView)
.setTitle("Form Elements")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@TargetApi(11)
public void onClick(DialogInterface dialog, int id) {
SpinnerDialogItemSelected(_spinner.getSelectedItem().toString());
dialog.dismiss();
}
}).show();
And then create a method for your dialog to call which lets you handle the result
private void SpinnerDialogItemSelected(String value){
// Do your thing here
}