erandi
erandi

Reputation: 317

Using multiple spinners in the same layout

In my android app,I have 4 spinners in the layout.I've set values to the spinners,using listeners. Following code snippet shows how i tried to do this. But it seems like not listening to the user selected values.Instead it gets the first value that is set in the spinner.I don't have good idea on the parameters defined in the onItemSelected() method. What is the mistake i am doing ?

I've edited my question.Please consider following code. It is using the same procedure to set values to the spinners, using different queries.

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

    final ArrayList<String> provinceList = new ArrayList<String>();
    final ArrayList<String> disList      = new ArrayList<String>();
    final ArrayList<String> divList      = new ArrayList<String>();
    final ArrayList<String> gramaList    = new ArrayList<String>();

    final Spinner disSpinner = (Spinner) findViewById(R.id.spinner2);
    final Spinner divSpinner = (Spinner) findViewById(R.id.spinner3);
    final Spinner gramaSpinner = (Spinner) findViewById(R.id.spinner4);


    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, provinceList);
    final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, disList);
    final ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, divList);
    final ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, gramaList);


    Button b = (Button) findViewById(R.id.button1);

    DBHelper dbhlper = new DBHelper(getApplicationContext());
    try {

        dbhlper.createDataBase();
        mydb = dbhlper.openDataBase();

    } catch (IOException e) {
        e.printStackTrace();
    }

    final String selected_item;
    String province_query = "SELECT pro_code,name FROM jos_province";

    Cursor c = mydb.rawQuery(province_query, null);
    provinceList.clear();

    final HashMap<String, Integer> hm = new HashMap<String, Integer>();
    if (c != null) {

        int i = 1;
        int noCols = c.getCount();
        if (c.moveToFirst()) {
            do {

                Log.d("Data" + i, c.getString(1));
                provinceList.add(c.getString(1));

                hm.put(c.getString(1), c.getInt(0));

            } while (c.moveToNext());

        }   

    }

    final Spinner provSpinner = (Spinner) findViewById(R.id.spinner1);
    // ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,
    // provinceList);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    provSpinner.setAdapter(adapter1);

    // //////////////////////////////// Handling spinner1 ///////////////////////////////////

    final String sel_district_id = null;

    provSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @TargetApi(11)
        @Override
        public void onItemSelected(AdapterView<?> parent, View arg1,
                int arg2, long arg3) {
            String province = provSpinner.getSelectedItem().toString();
            int sel_province_id = hm.get(province);



            String district = "SELECT id,name_english from jos_district `enter code here`where province_id= "+ sel_province_id;

            // ArrayList<String> disList=new ArrayList<String>();
            Cursor c = mydb.rawQuery(district, null);
            hm.clear();
            disList.clear();
            if (c != null) {

                int i = 1;
                int noCols = c.getCount();
                if (c.moveToFirst()) {
                    do {

                        Log.d("Data" + i, c.getString(1));

                        disList.add(c.getString(1));


                    disList.toString());
                        i++;
                        hm.put(c.getString(1),c.getInt(0));


                    } while (c.moveToNext());

                }

            }
            //String sel_district_id ="66";

            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            String listCount=Integer.toString(adapter2.getCount());
            disSpinner.setAdapter(adapter2);

                }

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

        }

    });

    disSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View arg1,
                int arg2, long arg3) {

            String selected_dis = `enter code here`disSpinner.getSelectedItem().toString();
            String sel_district_id = hm.get(selected_dis).toString();


            String division = "SELECT id,name_english FROM jos_division `enter code here`WHERE district_id= "
                    + sel_district_id;
            Cursor c = mydb.rawQuery(division, null);
            hm.clear();
            if (c != null) {

                int i = 1;
                int noCols = c.getCount();
                if (c.moveToFirst()) {
                    do {

                        Log.d("Data " + i, c.getString(1));
                        divList.add(c.getString(1));

                        hm.put(c.getString(1), `enter code here`c.getInt(0));
                        // `enter code here`provinceList.add(c.getString(1));

                    } while (c.moveToNext());

                }
            }
            // set the view for the Drop down list
            `enter code here`adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            // set the ArrayAdapter to the spinner
            divSpinner.setAdapter(adapter3);

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
    });

    divSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View arg1,
                int arg2, long arg3) {

            String `enter code here`selected_division=divSpinner.getSelectedItem().toString();

            sel_division_id = hm.get(selected_division).toString();

            String gramaniladhari = "SELECT lifecode,name_english FROM  jos_gramaniladhari WHERE divisional_secretariat_id= " + sel_division_id;


            hm.clear();
            Cursor c=mydb.rawQuery(gramaniladhari, null);
            if (c != null) {

                int i = 1;
                int noCols = c.getCount();
                if (c.moveToFirst()) {
                    do {

                        Log.d("Data " + i, c.getString(1));
                        divList.add(c.getString(1));

                        hm.put(c.getString(1),c.getInt(0));

                    } while (c.moveToNext());

                }

            }

        // set the view for the Drop down list
                         `enter code here`adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            // set the ArrayAdapter to the spinner
            gramaSpinner.setAdapter(adapter4);


            String `enter code here`selected_grama=gramaSpinner.getSelectedItem().toString();
            String lifeCode = hm.get(selected_grama).toString();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
    }); 

Following is the xml file of the layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="22dp"
    android:text="@string/enter_your_provincial_details"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="16dp"
    android:text="@string/button" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="40dp"
    android:text="@string/province"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/textView4"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="35dp"
    android:text="@string/district"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView3"
    android:layout_marginTop="44dp"
    android:text="@string/division"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView4"
    android:layout_marginTop="46dp"
    android:text="@string/grama_niladhari"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView3"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/button1" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView4"
    android:layout_alignLeft="@+id/spinner1"
    android:layout_alignParentRight="true" />

<Spinner
    android:id="@+id/spinner3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/spinner2"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/textView4" />

<Spinner
    android:id="@+id/spinner4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView5"
    android:layout_alignLeft="@+id/spinner3"
    android:layout_alignParentRight="true" />

   </RelativeLayout>

![This is the logcat result.the line 262 in my code is, String selected_grama = gramaSpinner.getSelectedItem().toString();

]1

Upvotes: 1

Views: 2386

Answers (2)

Shekhar Chikara
Shekhar Chikara

Reputation: 3822

Here in the code that you posted above, you are assigning the id of the same spinner i.e., spinner2 to both of your spinners in the code provSpinner and disSpinner. So, check out the ids of the resources from your layout XML file and then change the resource id for any one of the spinner. Hope that helps.

Upvotes: 1

guerilla
guerilla

Reputation: 405

You are using the resource with id spinner2 in both spinner objects. That could be the problem

Upvotes: 0

Related Questions