gpt4 book ai didi

java - Activity 旋转时,Viewmodel 显示空的回收 View

转载 作者:行者123 更新时间:2023-11-29 08:25:46 26 4
gpt4 key购买 nike

我对 Android 架构组件还很陌生,一直在尝试为我的服务器提供数据存储空间。问题是没有数据立即显示在回收站 View 上。在我的回收 View 上方有一个搜索 View (没有逻辑实现),当我单击搜索 View 进行输入时,回收 View 显示了所有应该早先显示的数据。

餐厅适配器:

public class RestaurantsAdapter extends RecyclerView.Adapter<RestaurantsAdapter.MyViewHolder> {

private List<Restaurant> data;
private Context context;
private LayoutInflater layoutInflater;
private final Random r = new Random();

public RestaurantsAdapter(Context context) {
this.data = new ArrayList<>();
this.context = context;
this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public RestaurantsAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_restaurant, parent, false);
return new RestaurantsAdapter.MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull RestaurantsAdapter.MyViewHolder holder, int position) {
holder.rName.setText(data.get(position).getName());
}

public void setData(List<Restaurant> newData) {
if (data != null) {
RestaurantDiffCallback restaurantDiffCallback = new RestaurantDiffCallback(data, newData);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(restaurantDiffCallback);

data.clear();
data.addAll(newData);
diffResult.dispatchUpdatesTo(this);
} else {
// first initialization
data = newData;
}
}

@Override
public int getItemCount() {
return data.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView rName;

public MyViewHolder(View itemView) {
super(itemView);
rName = (TextView) itemView.findViewById(R.id.restaurant_name);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
}
}

class RestaurantDiffCallback extends DiffUtil.Callback {

private final List<Restaurant> oldRestaurants, newRestaurants;

public RestaurantDiffCallback(List<Restaurant> oldPosts, List<Restaurant> newPosts) {
this.oldRestaurants = oldPosts;
this.newRestaurants = newPosts;
}

@Override
public int getOldListSize() {
return oldRestaurants.size();
}

@Override
public int getNewListSize() {
return newRestaurants.size();
}

@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldRestaurants.get(oldItemPosition).getIdentifier().equals(newRestaurants.get(newItemPosition).getIdentifier());
}

@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldRestaurants.get(oldItemPosition).equals(newRestaurants.get(newItemPosition));
}
}}

主要 Activity :

public class MainActivity extends AppCompatActivity {
private RestaurantsAdapter restaurantsAdapter;
private RestaurantViewModel restaurantViewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

restaurantsAdapter = new RestaurantsAdapter(this);

restaurantViewModel = ViewModelProviders.of(this).get(RestaurantViewModel.class);
restaurantViewModel.getAllRestaurants().observe(this, restaurants -> restaurantsAdapter.setData(restaurants));

RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(restaurantsAdapter);
}}

View 模型:

public class RestaurantViewModel extends AndroidViewModel {
private RestaurantDao restaurantDao;
private ExecutorService executorService;
private ApiInterface webService;

public RestaurantViewModel(@NonNull Application application) {
super(application);
restaurantDao = RestaurantsDatabase.getInstance(application).restaurantDao();
executorService = Executors.newSingleThreadExecutor();
webService = ApiClient.getApiClient().create(ApiInterface.class);
}

LiveData<List<Restaurant>> getAllRestaurants() {
refreshUser();
return restaurantDao.findAll();
}

private void refreshUser() {
executorService.execute(() -> {

int numOfRestaurants = restaurantDao.totalRestaurants();

if (numOfRestaurants < 30) {
Call<RestaurantsModel> call = webService.getRestaurants();
call.enqueue(new Callback<RestaurantsModel>() {
@Override
public void onResponse(@NonNull Call<RestaurantsModel> call, @NonNull Response<RestaurantsModel> response) {
restaurantDao.saveAll(response.body().getData().getData());
}

@Override
public void onFailure(@NonNull Call<RestaurantsModel> call, @NonNull Throwable t) {
}
});
}
});
}}

最佳答案

如果您不使用 DiffUtil 及其 diffResult.dispatchUpdatesTo(this);你应该做 notifyDataSetChanged()。在您的情况下,在 RestaurantsAdapter.setData 中添加一行:

// first initialization
data = newData;
notifyDataSetChanged();

关于java - Activity 旋转时,Viewmodel 显示空的回收 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53364467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com