Я пытаюсь показать костюмированный список адресов с помощью Google AutoComplete Api. Я попытался сохранить их в MutableLiveData, используя ViewModel вместо загрузчиков. Проблема в том, что я не могу очистить MutableLiveData. Что было бы лучше сделать это? поиск в сети я не нашел обновленную статью. спасибо
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PlacePredictionModelFactory factory = new PlacePredictionModelFactory(getApplication());
PlacePredictionViewModel placePredictionModel = ViewModelProviders.of(this,factory).get(PlacePredictionViewModel.class);
final Observer<ArrayList<Address>> placesObserver = places ->{
// Update the UI
addressesList = places;
//Adapter to adapt addresses onto AutoCompleteTextView
AddressAdapter addressAdapter = new AddressAdapter(getApplicationContext(),addressesList);
addressEditText.setAdapter(addressAdapter);
addressEditText.showDropDown();
};
mAutoCompleteEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Observe the LiveData
placePredictionModel.getPredictedPlaces()
.observe(MainActivity.this, placesObserver);
}
}
my ViewModel:
class PlacePredictionViewModel extends ViewModel {
private SingleLiveEvent<ArrayList<Address>> placesSingleEvent;
PlacePredictionViewModel(Application application){
application = application;
Context context = application.getApplicationContext();
// Initialize the SDK
Places.initialize(context, PLACES_KEY);
// Create a new Places client instance
placesClient = Places.createClient(context);
}
LiveData<ArrayList<Address>> getPredictedPlaces() {
if (placesSingleEvent == null) { placesSingleEvent = new SingleLiveEvent<>(); }
//my function to load the predicredplaces
loadPlacesfromIDs();
return placesSingleEvent;
}
loadPlacesfromIDs(){
placesClient.fetchPlace(fetchRequest).addOnSuccessListener((fetchResponse) -> {
//make operatoins to load the data then setValues
placesSingleEvent.setValue(addressArray);});
}
}