如何在数组列表中获取微调器值

时间:2013-04-24 07:18:18

标签: android arraylist spinner

我有这个代码,其中我有一个带有国家名称的微调器。我已经在strings.xml文件中声明了这些名称。现在,我想要的是当用户选择一个国家时,该值被发送到arraylist然后使用post参数我想执行一个xml文件。这是我的代码 - :

public class AddBank extends Activity implements
AdapterView.OnItemSelectedListener{

    EditText et1,et2,et3,et4,et5,et6,et7,et8,et9;
    Spinner spinner1;
    Button add;
    String userpin;
    TextView country;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addbank);

        Bundle gotuser= getIntent().getExtras();
        userpin= gotuser.getString("username");  

      et1=(EditText)findViewById(R.id.editText1);  
      et2=(EditText)findViewById(R.id.editText2);
      et3=(EditText)findViewById(R.id.editText3);
      et4=(EditText)findViewById(R.id.editText4);
      et5=(EditText)findViewById(R.id.editText5);
      et6=(EditText)findViewById(R.id.editText6);
      et7=(EditText)findViewById(R.id.editText7);
      et8=(EditText)findViewById(R.id.editText8);
      et9=(EditText)findViewById(R.id.editText9);
      spinner1=(Spinner)findViewById(R.id.spinner1);
      add=(Button)findViewById(R.id.button1);
      country=(TextView)findViewById(R.id.textView11);

      ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
              this, R.array.country_array, android.R.layout.simple_spinner_item);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner1.setAdapter(adapter);


         add.setOnClickListener(new OnClickListener() { 


                @SuppressWarnings("deprecation")
                public void onClick(View v) { 

                    if(et1.getText().toString().equals("")){
                        AlertDialog alertDialog = new AlertDialog.Builder(AddBank.this).create();
                        alertDialog.setTitle("oops!");
                        alertDialog.setMessage("Account Type field is empty");
                        alertDialog.setButton("Ok",
                        new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                  //dismiss the dialog  
                                }
                            });
                        alertDialog.show();
                    }
                    else if(et2.getText().toString().equals("")){
                        AlertDialog alertDialog = new AlertDialog.Builder(AddBank.this).create();
                        alertDialog.setTitle("oops!");
                        alertDialog.setMessage("Bank Name field is empty");
                        alertDialog.setButton("Ok",
                        new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                  //dismiss the dialog  
                                }
                            });
                        alertDialog.show();
                    }
                    else if(et3.getText().toString().equals("")){
                        AlertDialog alertDialog = new AlertDialog.Builder(AddBank.this).create();
                        alertDialog.setTitle("oops!");
                        alertDialog.setMessage("Account Name field is empty");
                        alertDialog.setButton("Ok",
                        new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                  //dismiss the dialog  
                                }
                            });
                        alertDialog.show();
                    }
                    else if(et4.getText().toString().equals("")){
                        AlertDialog alertDialog = new AlertDialog.Builder(AddBank.this).create();
                        alertDialog.setTitle("oops!");
                        alertDialog.setMessage("Account Number field is empty");
                        alertDialog.setButton("Ok",
                        new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                  //dismiss the dialog  
                                }
                            });
                        alertDialog.show();
                    }
                    else if(et5.getText().toString().equals("")){
                        AlertDialog alertDialog = new AlertDialog.Builder(AddBank.this).create();
                        alertDialog.setTitle("oops!");
                        alertDialog.setMessage("Bank Address field is empty");
                        alertDialog.setButton("Ok",
                        new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                  //dismiss the dialog  
                                }
                            });
                        alertDialog.show();
                    }
                    else if(et6.getText().toString().equals("")){
                        AlertDialog alertDialog = new AlertDialog.Builder(AddBank.this).create();
                        alertDialog.setTitle("oops!");
                        alertDialog.setMessage("Bank City field is empty");
                        alertDialog.setButton("Ok",
                        new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                  //dismiss the dialog  
                                }
                            });
                        alertDialog.show();
                    }
                    else if(et7.getText().toString().equals("")){
                        AlertDialog alertDialog = new AlertDialog.Builder(AddBank.this).create();
                        alertDialog.setTitle("oops!");
                        alertDialog.setMessage("Bank Zip field is empty");
                        alertDialog.setButton("Ok",
                        new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                  //dismiss the dialog  
                                }
                            });
                        alertDialog.show();
                    }
                    else if(et8.getText().toString().equals("")){
                        AlertDialog alertDialog = new AlertDialog.Builder(AddBank.this).create();
                        alertDialog.setTitle("oops!");
                        alertDialog.setMessage("Bank SWIFT field is empty");
                        alertDialog.setButton("Ok",
                        new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                  //dismiss the dialog  
                                }
                            });
                        alertDialog.show();
                    }
                    else if(et9.getText().toString().equals("")){
                        AlertDialog alertDialog = new AlertDialog.Builder(AddBank.this).create();
                        alertDialog.setTitle("oops!");
                        alertDialog.setMessage("Bank IFSC field is empty");
                        alertDialog.setButton("Ok",
                        new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                  //dismiss the dialog  
                                }
                            });
                        alertDialog.show();
                    }
                    else {

                            AlertDialog alertDialog = new AlertDialog.Builder(AddBank.this).create(); 
                            alertDialog.setTitle("ModeooPay:");
                            alertDialog.setMessage("Are you sure you want to send this money?");
                            alertDialog.setButton( Dialog.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

                                postParameters.add(new BasicNameValuePair("femail",  
                                        et1.getText().toString()));

                                postParameters.add(new BasicNameValuePair("amount",   
                                        et2.getText().toString()));

                                postParameters.add(new BasicNameValuePair("des",   
                                        et3.getText().toString()));

                                postParameters.add(new BasicNameValuePair("des",   
                                        et4.getText().toString()));

                                postParameters.add(new BasicNameValuePair("des",   
                                        et5.getText().toString()));

                                postParameters.add(new BasicNameValuePair("des",   
                                        et6.getText().toString()));

                                postParameters.add(new BasicNameValuePair("des",   
                                        et7.getText().toString()));

                                postParameters.add(new BasicNameValuePair("des",   
                                        et8.getText().toString()));

                                postParameters.add(new BasicNameValuePair("des",   
                                        et9.getText().toString()));

                                postParameters.add(new BasicNameValuePair("des",   
                                        country.getText().toString()));

                                postParameters.add(new BasicNameValuePair("username",  
                                        userpin));

                                 /*            String valid = "1";*/      

                                String response = null;



                                try {

                                   response = CustomHttpClient.executeHttpPost("http://192.168.0.2:11132/androidxml/confirmtransfer.php",postParameters);

                                   String res = response.toString();

                                   //res = res.trim();

                                   res = res.replaceAll("\\s+","");
                                   Log.i("log_tag", "reedem voucher  " + res);

                                   // error.setText(res);

                                   if (res.equals("1")) {



                                       AlertDialog alertDialog = new AlertDialog.Builder(AddBank.this).create();
                                        alertDialog.setTitle("Successfull");
                                        alertDialog.setMessage("Bank Added Successfully.");
                                        alertDialog.setButton("Ok",
                                        new DialogInterface.OnClickListener() {
                                              public void onClick(DialogInterface dialog, int which) {
                                                  dialog.cancel();
                                              }
                                        });
                                        alertDialog.show();



                                   }


                                else {

                                       AlertDialog alertDialog = new AlertDialog.Builder(AddBank.this).create();
                                        alertDialog.setTitle("Authentication");
                                        alertDialog.setMessage("Bank with this information already exists.");
                                        alertDialog.setButton("Ok",
                                        new DialogInterface.OnClickListener() {
                                              public void onClick(DialogInterface dialog, int which) {
                                                 dialog.cancel();
                                              }
                                        });
                                        alertDialog.show();
                                   }

                                } 

                                catch (Exception e) {


                                }

                               }
                               });

                            alertDialog.setButton( Dialog.BUTTON_NEGATIVE, "NO", new DialogInterface.OnClickListener()    {
                                  public void onClick(DialogInterface dialog, int which) {

                                      dialog.cancel();

                                  }
                                  });

                                alertDialog.show();


                    }
                }

             });   


    }

    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        country.setText(country_array[arg2]);
        // TODO Auto-generated method stub

    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
        country.setText("");

    }

}

现在,在检索微调器的值的方法中,我收到“country_array无法解析为变量”的错误。 请帮帮我。谢谢。

3 个答案:

答案 0 :(得分:3)

希望这可以帮到你。

Resources res = getResources();
String[] country_array = res.getStringArray(R.array.country_array);
.....    
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    country.setText(country_array[arg2]);

}

答案 1 :(得分:0)

在您的活动中声明country_array:

AdapterView.OnItemSelectedListener{

EditText et1,et2,et3,et4,et5,et6,et7,et8,et9;
Spinner spinner1;
Button add;
String userpin;
TextView country;

//add this
String [] country_array;

@Override
public void onCreate(Bundle savedInstanceState) {
...
country_array = res.getStringArray(R.array.country_array);
...
}

答案 2 :(得分:-1)

将数组传递给数组适配器时,请使用此getResources()。getStringArray(R.array.country_array)