简体   繁体   中英

How to reset spinner list when fragment resume?

I have two spinner in my application which populating list from sqlite and it is working fine. When I select State in first spinner then second spinner list populate depend on first spinner. Now what I want that when I select item in both spinner and then I press back and again launch the activity then the list of second spinner still there. I want to clear list of second spinner when I press back button and Populate list in second spinner when user select value in first spinner.

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

        }

    }

add this method to activity.Hope this will work .

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

}

Try using

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

Use Onresume for clear the array list you want

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

or in onCreate view After Inttilizing array clean both arrayList.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM