LiveData не обновляется после первого звонка в Android Java - PullRequest
1 голос
/ 03 мая 2019

Я пытаюсь создать приложение для получения списка каналов с сервера и отображения в Recyclerview.Я пытаюсь реализовать базовую реализацию LiveData следующим образом.

Я настроил наблюдателя в своем фрагменте следующим образом:

viewModel = ViewModelProviders.of(getActivity()).get(SellViewModel.class);

    viewModel.getSellItemList(19).observe(this, new Observer<List<LambdaSellRequestClass>>() {
            @Override
            public void onChanged(@Nullable List<LambdaSellRequestClass> sellItems) {


                adapter.setSellEntities(sellItems);

            }
        });

Мои классы SellViewModel выглядят так:

public class SellViewModel extends AndroidViewModel {

private SellRepository repository;

private MutableLiveData<List<LambdaSellRequestClass>> sellItems;

public SellViewModel(@NonNull Application application) {
    super(application);
    repository = new SellRepository(application);

    try {
        if (sellItems == null) {
            sellItems = new MutableLiveData<>();
            sellItems.postValue(repository.getSellItemList(user_id));
        }
    }catch (Exception e) {
        Log.d("SELLFRAGMENT", "Error: " + e.getLocalizedMessage());
    }
}

public MutableLiveData<List<LambdaSellRequestClass>> getSellItemList(int userId) throws ExecutionException, InterruptedException {

    return sellItems;
}

}

Мой SellRepository, как этот:

public class SellRepository {

public SellRepository(Application application) {

}

    public List<LambdaSellRequestClass> getSellItemList(int userId) throws ExecutionException, InterruptedException {

        return new SellRepository.GetSellItemListAsync(SellRepository.this).execute(userId).get();

    }

   private static class GetSellItemListAsync extends AsyncTask<Integer, Void, List<LambdaSellRequestClass>> {

        List<LambdaSellRequestClass> list = new ArrayList<>();      

        public GetSellItemListAsync() {

        }

        @Override
        protected List<LambdaSellRequestClass> doInBackground(Integer... integers) {

            final int userID = integers[0];


          list =     

     lambdaFunctionsCalls.getSellItemByUser_lambda(requestClass).getSellItems();

            return list;
        }
  }

Моя проблема заключается в том, что я добавляю новые предметы продажи в базу данных, а это не обновляет мобильное приложение.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...