[英]LiveData with ModelView not updating adapter
我正在學習新組件,並嘗試將LiveData與DataBinding和ModelView一起使用。 我在recyclerview上顯示項目時有問題(更新UI)。 這是我的ModelView:
public class PlacesViewModel extends ViewModel {
private MutableLiveData<List<Place>> placeList;
private Repository repository;
public PlacesViewModel() {
repository = Repository.getInstance();
}
public LiveData<List<Place>> getPlaceList() {
if (placeList == null) {
placeList = new MutableLiveData<List<Place>>();
getPlaces();
}
return placeList;
}
private void getPlaces() {
repository.getLocationsFromBackend(new DataSource.GetLocationsCallback() {
@Override
public void onSuccess(MutableLiveData<List<Place>> body) {
if (body != null) {
placeList = body;
} else {
placeList = new MutableLiveData<>();
}
}
@Override
public void onError(int code) {
}
@Override
public void onUnknownError() {
}
@Override
public void onNoInternet() {
}
@Override
public void onNoServer() {
}
@Override
public void onAlreadyPairedError() {
}
@Override
public void onNoCustomerError() {
}
@Override
public void onLoadIndicator(boolean active) {
}
@Override
public void reAuthenticate() {
}
});
}
問題來了,當我啟動該應用程序時,屏幕為“空”。 但是,當我處於調試模式並且在getPlaceList()行中時,我可以切換到后台線程以使用改造執行api調用。 只有比它有效...
這是我的Repository單例方法:
public class Repository implements DataSource {
private static Repository INSTANCE;
private ApiService service = new RetrofitFactory().getService();
private static final int RESPONSE_OK = 0;
private final MutableLiveData<List<Place>> data = new MutableLiveData<>();
public synchronized static Repository getInstance() {
if (INSTANCE==null) {
INSTANCE=new Repository();
}
return(INSTANCE);
}
private Repository() {
}
private <T extends BaseResponse> void handleActualResult(BaseLoadCallback<T> dataSource,
MutableLiveData<List<Place>> actualResult) {
final int resultCode = 0;
switch (resultCode) {
case RESPONSE_OK:
dataSource.onSuccess(actualResult);
break;
default:
dataSource.onError(resultCode);
break;
}
}
@Override
public void getLocationsFromBackend(GetLocationsCallback presener) {
final SecurityRequest request = new SecurityRequest();
service.getPlaces(request).enqueue(new Callback<GetPlacesResponse>() {
@Override
public void onResponse(@NonNull Call<GetPlacesResponse> call, @NonNull Response<GetPlacesResponse> response) {
if (response.isSuccessful()) {
if (response.body() != null) {
data.postValue(response.body().getLokali());
handleActualResult(presener, data);
}
}
}
@Override
public void onFailure(@NonNull Call<GetPlacesResponse> call, @NonNull Throwable t) {
Log.d("Error: ", t.getMessage());
}
});
}
@Override
public List<Place> getLocations() {
return null;
}
}
它轉到onResposne,然后返回getPlaceList()並返回包含對象的填充列表。 這是我制作viewmodel實例並觀察LiveData對象的方法:
public class LocationFragment extends Fragment {
private PlacesViewModel placesViewModel;
private RecyclerView recyclerView;
private PlaceCustomAdapter customAdapter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.recyclerViewId);
placesViewModel = ViewModelProviders.of(LocationFragment.this).get(PlacesViewModel.class);
customAdapter = new PlaceCustomAdapter(getLayoutInflater());
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
recyclerView.setHasFixedSize(false);
placesViewModel.getPlaceList().observe(this, places -> {
customAdapter.setList(places);
recyclerView.setAdapter(customAdapter);
});
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return(inflater.inflate(R.layout.activity_main, container, false));
}
這也是我的適配器:
public class PlaceCustomAdapter extends RecyclerView.Adapter<PlaceCustomAdapter.CustomLocationViewHolder> {
private LayoutInflater layoutInflater;
private List<Place> placeList;
public PlaceCustomAdapter(LayoutInflater layoutInflater) {
this.layoutInflater = layoutInflater;
}
@NonNull
@Override
public PlaceCustomAdapter.CustomLocationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.single_item_row_recycler_view, parent, false);
return new PlaceCustomAdapter.CustomLocationViewHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull PlaceCustomAdapter.CustomLocationViewHolder holder, int position) {
Place p = placeList.get(position);
holder.bind(p);
}
void setList(List<Place> placeList) {
this.placeList = placeList;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return(placeList == null ? 0 : placeList.size());
}
public class CustomLocationViewHolder extends RecyclerView.ViewHolder {
private final ViewDataBinding binding;
public CustomLocationViewHolder(ViewDataBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void bind(Object obj) {
binding.setVariable(BR.model, obj);
binding.executePendingBindings();
}
}
因此,它不會崩潰,只有回收站是空的...如果您需要任何其他說明,請詢問。 我將不勝感激任何幫助。 謝謝
我只是想知道出了什么問題。 我正在調用方法handleActualResult(presener,data); 在錯誤的地方。 相反,在onSuccess中,我將其投入處理響應中...
@Override
public void getLocationsFromBackend(GetLocationsCallback presener) {
final SecurityRequest request = new SecurityRequest();
service.getPlaces(request).enqueue(new Callback<GetPlacesResponse>() {
@Override
public void onResponse(@NonNull Call<GetPlacesResponse> call, @NonNull Response<GetPlacesResponse> response) {
if (response.isSuccessful()) {
if (response != null) {
data.postValue(response.body().getLokali());
} else {
data = new MutableLiveData<>();
}
}
}
@Override
public void onFailure(@NonNull Call<GetPlacesResponse> call, @NonNull Throwable t) {
Log.d("Error: ", t.getMessage());
}
});
handleActualResult(presener, data);
}
如果有人遇到同樣的問題,請...享受!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.