1

In android application to populate values into spinner I have used values from values\strings.xml

 <string-array name="myList">
       <item>first</item>
        <item>second</item>
  </string-array>

In layout\input_box.xml

 <Spinner
        android:id="@+id/spinner_problems"
        android:entries="@array/myList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"       
         />

above code will populate spinner values from myList

on MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {

  spin_problem = (Spinner)findViewById((R.id.spinner_problems));   
        spin_problem.setOnItemSelectedListener(new OnItemSelectedListener(){

             @Override
             public void onItemSelected(AdapterView<?> adapter, View view1, int pos, long id)
             {
                  spin_problem_val= adapter.getItemAtPosition(pos).toString();
             }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

    //thread run
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                try {
                    showInputDialog("Any Complaints?");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }}, 8000);  // 3000 milliseconds
        //end thread        

}

showInputDailog method code // my textbox and spinner is in this method

public void showInputDialog(final String textparam) throws JSONException {      
         try{
        // get prompts.xml view to show complaint popup 
        LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
        View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                MainActivity.this);
        alertDialogBuilder.setView(promptView);
         if(textparam.toString() == "Feedback"){alertDialogBuilder.setTitle("Feedback");}
         else{alertDialogBuilder.setTitle("Any Complaints?");}

        //Get all dialog box values
        final EditText name = (EditText) promptView
                .findViewById(R.id.name);
        final EditText mobno = (EditText) promptView
                .findViewById(R.id.mobno);
        final EditText complaint = (EditText) promptView
                .findViewById(R.id.complaint);



        // final Spinner problems = (Spinner)findViewById((R.id.spinner_problems));     
        // final Spinner area = (Spinner)findViewById((R.id.spinner_area));

    //  final String aa = problems.getSelectedItem().toString();
     // final String vvv = area.getSelectedItem().toString();   

//HERE I need to get spinner value to process further... }

Neo
  • 15,491
  • 59
  • 215
  • 405
  • 1
    did you add `onItemSelectedListener` to your spinner? – aether643 Aug 14 '14 at 16:00
  • no whats that ? is it necessary ? i'm pretty new to android development .. can you help me in this ? – Neo Aug 14 '14 at 16:09
  • 1
    Did you set the spinner adapter? – lucianohgo Aug 14 '14 at 16:10
  • No I did not... I only write above code .. and I can see values (first,second) in spinner dropdown. now I want to select those values which is selected by user and do operation on that value for example save into database thanks in advance :) – Neo Aug 14 '14 at 16:13

1 Answers1

1

to get values from spinner, add onItemSelectedListener to your spinner. Like this :

     String value;
     final Spinner problems = (Spinner)findViewById((R.id.spinner_problems));
      problems.setOnItemSelectedListener(new OnItemSelectedListener(){

         @Override
         public void onItemSelected(AdapterView<?> adapter, View view1, int pos, long id)
         {
              value= adapter.getItemAtPosition(pos).toString();
         }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

Edit :

-For more information check this, this, and this.

Edit 2 :

  • if your spinner is in your main_activity, add the code inside your onCreate method with value as global variable.

  • if you want your spinner in an alertDialog, add the code inside your showInputDialog method

Community
  • 1
  • 1
aether643
  • 190
  • 8
  • thanks I able to resolve it by following code LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this); View promptView = layoutInflater.inflate(R.layout.input_dialog, null); final Spinner problems = (Spinner) promptView.findViewById((R.id.spinner_problems)); spin_problem_val = problems.getSelectedItem().toString(); – Neo Aug 15 '14 at 06:39