Reputation: 517
I would like to get the value of the item selected in a spinner inside an AlertDialog, after pressing the PositiveButton. My function:
public void open_and_get() {
View dialog_filtri=View.inflate(this,R.layout.finestra_filtri,null);
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Filtra per");
builder.setView(dialog_filtri);
builder.setCancelable(true);
builder.setPositiveButton("Filtra",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//All the spinners are inside a Linear layout, so i would like to get it and slide inside his spinner child to get them parameters.
View view=(View) LayoutInflater.from(getApplicationContext()).inflate(R.layout.finestra_filtri,null);
LinearLayout l= (LinearLayout) view.findViewById(R.id.linear_con_filtri);
String[] parametri=new String[l.getChildCount()];
int i;
for(i=0;i<l.getChildCount();i++) {
if(l.getChildAt(i) instanceof Spinner) {
Spinner temp=(Spinner)l.getChildAt(i);
parametri[i]=temp.getSelectedItem().toString();
}
}
Log.d(TAG,""+parametri[0]+"\n"+parametri[1]);
}
});
builder.setNegativeButton("Cancella",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
builder.show();
}
The problem is that i can get only the default values of the spinner, maybe because i am not referring to the correct spinner but to a new one.
Upvotes: 0
Views: 870
Reputation: 109247
On this code line,
View view=(View) LayoutInflater.from(getApplicationContext()).inflate(R.layout.finestra_filtri,null);
You are inflating new view of Alert Dialog and referencing to it, not a shown one,
So Remove code line,
View view=(View) LayoutInflater.from(getApplicationContext()).inflate(R.layout.finestra_filtri,null);
and change line,
LinearLayout l= (LinearLayout) view.findViewById(R.id.linear_con_filtri);
with
LinearLayout l= (LinearLayout) dialog_filtri.findViewById(R.id.linear_con_filtri);
Upvotes: 1