无法在AlertDialog Android

时间:2016-06-16 14:24:05

标签: android spinner alertdialog

我想在按下PositiveButton之后获取在AlertDialog内的微调器中选择的项目的值。 我的功能:

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

问题是我只能获得微调器的默认值,可能是因为我没有提到正确的微调器但是指的是新的微调器。

1 个答案:

答案 0 :(得分:1)

在此代码行中,

View view=(View) LayoutInflater.from(getApplicationContext()).inflate(R.layout.finestra_filtri,null);

您正在夸大警报对话框的新视图并引用它,而不是显示的,

所以删除代码行,

View view=(View) LayoutInflater.from(getApplicationContext()).inflate(R.layout.finestra_filtri,null);

并更改行

LinearLayout l= (LinearLayout) view.findViewById(R.id.linear_con_filtri);

LinearLayout l= (LinearLayout) dialog_filtri.findViewById(R.id.linear_con_filtri);