片段恢复时如何重置微调器列表?

时间:2016-10-19 06:40:27

标签: android sqlite spinner

我的应用程序中有两个微调器,它从sqlite填充列表,它工作正常。当我在第一个微调器中选择State时,第二个微调器列表填充依赖于第一个微调器。现在我想要的是当我在两个微调器中选择项目然后我按回然后再次启动活动然后第二个微调器的列表仍在那里。当我在第一个微调器中用户选择值时,当我按下后退按钮并在第二个微调器中填充列表时,我想要清除第二个微调器的列表。

public void fillStateData() {
        try {
            ArrayList<String> state_array = new ArrayList<String>();
            state_array.add("Select State");
            Cursor cursor_State = db.rawQuery("SELECT nSerialNo as _id,cCodeName FROM CodeMaster where nCtgId = 6", null);


            if (cursor_State.moveToFirst()) {
                do {
                    //assing values
                    String stateID = cursor_State.getString(0);
                    String stateName = cursor_State.getString(1);
                    stateData = stateName;
                    state_array.add(stateData);
                } while (cursor_State.moveToNext());
            }
            ArrayAdapter my_Adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, state_array);
            spnState.setAdapter(my_Adapter);
            cursor_State.close();
            spnState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                    state = spnState.getSelectedItem().toString();

                    Cursor cursor = db.rawQuery("SELECT nSerialNo FROM CodeMaster where cCodeName = '" + state + "'", null);

                    if (cursor.moveToFirst()) {
                        stateCodeId = cursor.getString(0);
                    }
                    cursor.close();

                    fillDistrictData(stateCodeId);
                }

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

                }
            });
        } catch (Exception e) {

            e.printStackTrace();

        }
    }

    public void fillDistrictData(String stateCodeId) {
        try {
            district_array = new ArrayList<String>();
            district_array.clear();
            district_array.add("Select District");
            Cursor cursor_District = db.rawQuery("SELECT nSerialNo as _id,cCodeName FROM CodeMaster where nParentSerialNo = '" + stateCodeId + "'", null);


            if (cursor_District.moveToFirst()) {
                do {
                    //assing values
                    String districtID = cursor_District.getString(0);
                    String districtName = cursor_District.getString(1);
                    districtData = districtName;
                    district_array.add(districtData);


                } while (cursor_District.moveToNext());
            }
            district_Adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, district_array);
            spnDistrict.setAdapter(district_Adapter);
            cursor_District.close();
            spnDistrict.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                    district = spnDistrict.getSelectedItem().toString();

                    Cursor cursor = db.rawQuery("SELECT nSerialNo FROM CodeMaster where cCodeName = '" + district + "'", null);

                    if (cursor.moveToFirst()) {
                        do {
                            //assing values
                            districtCodeId = cursor.getString(0);

                        } while (cursor.moveToNext());
                    }
                    cursor.close();

                    fillTalukaData(districtCodeId);

                }

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

                }


            });
        } catch (Exception e) {

            e.printStackTrace();

        }

    }

3 个答案:

答案 0 :(得分:0)

将此方法添加到activity.Hope这将起作用。

@Override
public void onBackPressed() {
   spnDistrict.getAdapter().clear();
   spnDistrict.getAdapter().notifyDataSetChanged();
   finish();

}

答案 1 :(得分:0)

尝试使用

 adapter.clear();
 spinner.setAdapter(new ArrayAdapter<String>(YourActivity.this,android.R.layout.simple_dropdown_item_1line,adapter));

答案 2 :(得分:0)

使用Onresume清除您想要的数组列表

 @Override
  public void onResume() {
     Log.e("DEBUG", "onResume of LoginFragment");
state_array.clear();
      district_array.clear();
     super.onResume();
  }

或在onCreate视图中使用Inttilizing数组后清除两个arrayList。