【问题标题】:How to reset spinner list when fragment resume?片段恢复时如何重置微调器列表?
【发布时间】:2017-02-28 15:45:36
【问题描述】:

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

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

        }

    }

【问题讨论】:

  • 你用哪种方法设置你的微调器?
  • 我在 onCreateView 方法中调用 fillStateData(),当用户选择选择状态时,取决于选择的状态,我正在填充区。
  • 尝试将其移至片段的 onResume()。
  • 我试过了,但它不起作用。实际上,当我按下后退按钮并移动到我的主要活动然后再次移动到这个活动时,我有两个片段,我将这些微调器放在第一个片段上,然后第二个微调器的列表仍然存在。当用户再次启动活动时,我想将微调器设为空白。

标签: android sqlite spinner


【解决方案1】:

将此方法添加到活动中。希望这会起作用。

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

}

【讨论】:

  • 感谢 Dharmistha,但它在 Fragment 中不起作用。
【解决方案2】:

尝试使用

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

【讨论】:

    【解决方案3】:

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

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

    或在 onCreate 视图中 Inttilizing array 清理两个 arrayList。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多