0

I have this code in which I have a spinner with country names in it.I have declared these names in strings.xml file.Now,what I want is that when user selects a country the value is sent to arraylist and then using post parameter I want to execute an xml file.Here is my code-:

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

    }

}

Now,in the method to retrieve the value of spinner I am getting this error that "country_array cannot be resolved to a variable." Please help me out here.Thanks in advance.

user218554
  • 49
  • 1
  • 1
  • 5

3 Answers3

3

Hope this can help you.

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]);

}
midhunhk
  • 5,560
  • 7
  • 52
  • 83
JaveZh
  • 184
  • 3
0

Declare country_array in your activity:

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);
...
}
Neoh
  • 15,906
  • 14
  • 66
  • 78
-1

While passing array to your array adapter give this:

getResources().getStringArray(R.array.country_array)
Saeed
  • 3,294
  • 5
  • 35
  • 52
Shrikant
  • 1,560
  • 1
  • 15
  • 32
  • this is wrong, user is doing right @shrikant, problem is that there is not defne spinner click listner – stackoverflow Apr 24 '13 at 07:25
  • his list is shown because in my case same code is properly working, but problem is user does not define click listner of spinner then hhow user can get value of spinner – stackoverflow Apr 24 '13 at 07:29
  • @stackoverflow: he will get the value from onitemSelected method on not from any clicklistener, check this http://stackoverflow.com/a/1948187/1457952 – Shrikant Apr 24 '13 at 07:32
  • may i knw where user define this .......... spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { – stackoverflow Apr 24 '13 at 07:33
  • @stackoverflow: i guess you are new to Java and android, he has already implemented AdapterView.OnItemSelectedListener. – Shrikant Apr 24 '13 at 07:34
  • ok, but your and not downvote by me because i dnt have 125 points – stackoverflow Apr 24 '13 at 07:35