1

在我的 android 应用程序中,布局中有 4 个微调器。我使用侦听器为微调器设置了值。以下代码片段显示了我如何尝试执行此操作。但似乎没有听用户选择的值。相反,它获取了微调器中设置的第一个值。我对 onItemSelected() 方法中定义的参数不太了解。我在做什么错?

我已经编辑了我的问题。请考虑以下代码。它使用相同的过程来设置微调器的值,使用不同的查询。

@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) {

        }
    }); 

以下是布局的xml文件

<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>

![这是 logcat 结果。我的代码中的第 262 行是 String selected_grama = gramaSpinner.getSelectedItem().toString();

] 1

4

2 回答 2

1

在您上面发布的代码中,您将id相同的微调器分配给spinner2代码中的两个微调器provSpinnerdisSpinner. 因此,从您的布局 XML 文件中检查资源的 id,然后更改任何一个微调器的资源 id。希望有帮助。

于 2012-09-26T04:17:13.453 回答
0

您在两个微调器对象中都使用了 id spinner2 的资源。这可能是问题所在

于 2012-09-26T04:14:32.120 回答