Задать вопрос
  • В android приложении значение в полях сохраняются при перезаходе на экран, хотя не должны. Как это исправить?

    @TEILabs Автор вопроса
    Мне помогло такое изменение кода с фильтрами:
    showFiltersBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ArrayList<String> cities = new ArrayList<>();
                    try {
                        Pair<ArrayList<String>, Integer> cityListGetResult = objectDataExchange.cityListGet(getContext(), sharedPreferences.getLong("id", 0));
                        if (cityListGetResult.second != null) {
                            toastCreator.createToast(getContext(), getLayoutInflater(), getResources().getStringArray(R.array.errors)[cityListGetResult.second]);
                            return;
                        }
                        else {
                            cities.addAll(cityListGetResult.first);
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }
                    cities.add(getContext().getString(R.string.ObjectCityFilterEdit));
                    ArrayAdapter<String> citiesAdapter = new ArrayAdapter<String>(getContext(), R.layout.city_dropdown_item, cities) {
                        @Override
                        public View getView(int position, View convertView, ViewGroup parent) {
    
                            View v = super.getView(position, convertView, parent);
                            if (position == getCount()-1) {
                                ((TextView)v.findViewById(android.R.id.text1)).setText("");
                                ((TextView)v.findViewById(android.R.id.text1)).setHint(cities.get(getCount()-1)); //"Hint to be displayed"
                            }
    
                            return v;
                        }
    
                        @Override
                        public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                            View v = super.getDropDownView(position, convertView, parent);
                            if (position == getCount()-1) {
                                return new View(getContext());
                            }
                            return v;
                        }
    
                        @Nullable
                        @Override
                        public String getItem(int position) {
                            return position != getCount() - 1 ? super.getItem(position) : "";
                        }
    
                        @Override
                        public int getCount() {
                            return super.getCount(); // you dont display last item. It is used as hint.
                        }
                    };
                    filtersCard.setVisibility(View.VISIBLE);
                    closeFiltersBtn.setVisibility(View.VISIBLE);
                    cityFilterSpinner.setAdapter(citiesAdapter);
                    cityFilterSpinner.setSelection(citiesAdapter.getCount()-1);
                    addressIndexFilterEdit.setText(null);
                    cityFilterSpinner.setSelection(citiesAdapter.getPosition(filters.get("city")));
                    addressIndexFilterEdit.setText(filters.get("addressIndex"));
                    //mainActivity.getProfileBtn().setVisibility(View.GONE);
                    clearFiltersBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            cityFilterSpinner.setSelection(citiesAdapter.getCount());
                            addressIndexFilterEdit.setText(null);
                            filters.replace("addressIndex", getContext().getString(R.string.ObjectCityFilterEdit));
                            filters.replace("city", null);
                        }
                    });
                    closeFiltersBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            filtersCard.setVisibility(View.GONE);
                            closeFiltersBtn.setVisibility(View.GONE);
                        }
                    });
                    applyFiltersBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            filters.replace("addressIndex", addressIndexFilterEdit.getText().toString());
                            filters.replace("city", cityFilterSpinner.getSelectedItem().toString());
                            cityFilterSpinner.setSelection(citiesAdapter.getCount());
                            addressIndexFilterEdit.setText(null);
                            filtersCard.setVisibility(View.GONE);
                            closeFiltersBtn.setVisibility(View.GONE);
                            ShowObjects(searchPlain.getText().toString(), linear, sharedPreferences, ft);
                            //mainActivity.getProfileBtn().setVisibility(View.VISIBLE);
                        }
                    });
                }
            });
    Ответ написан
    Комментировать