因此,出于某种原因,每当我向ArrayList添加一个新项目并通知recyclelerview它已被添加时,它就会复制先前条目的数据。因此,视图会被创建,但最后一个条目的数据不是当前数据。
奇怪的是,我使用ORM Sugar保存项目,因此当我检索它们时,会显示正确的数据。这表明数据已添加但视图未正确显示。
我只能假设每当我使用方法notifyItemInserted 时,recyclelerview都没有调用onBindViewHolder,因为当我使用notifyDataSetChange()时会显示正确的数据
这是我添加新项目的方法
@Override
public void AddCalorieToHistoryList(int calorieAmount, int currentCalorieAmount) {
// Get the date and the calorie amount from what the user entered.
Date date = new Date();
// Create the calorie history item and then save it (via ORM Sugar Library)
CalorieHistory calorieHistory = new CalorieHistory(date.toString(), calorieAmount);
calorieHistory.save();
// Notify the view of the new item that should be inserted.
historyView.InsertNewListItem(calorieHistory);
SubtractFromCurrentCalorieAmount(calorieAmount, currentCalorieAmount);
}
historyView的方法
@Override
public void InsertNewListItem(CalorieHistory calorieHistoryItem) {
// Add the new item
calorieHistories.add(calorieHistoryItem);
// Notify the insert so we get the animation from the default animator
historyListAdapter.notifyItemInserted(0);
}
这里是recyclerview适配器
public class HistoryListAdapter extends RecyclerView.Adapter<HistoryListAdapter.HistoryListViewHolder> {
List<CalorieHistory> calorieHistoryList;
public HistoryListAdapter(List<CalorieHistory> historyList) {
this.calorieHistoryList = historyList;
}
// I think this is run once, it generates the view holder from the layout that we are using for each list item.
// This way it won't have to grab it each time we make a new list item. It's all stored on our view holder.
@Override
public HistoryListViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.calorie_history_item, parent, false);
return new HistoryListViewHolder(itemView);
}
// This executes everytime we make a new list item, it binds the data to the view.
@Override
public void onBindViewHolder(HistoryListViewHolder holder, int position) {
// Get the current calorHistory object and set it's data to the views.
CalorieHistory calorieHistory = calorieHistoryList.get(position);
holder.tvDate.setText(calorieHistory.date);
holder.tvAmount.setText(String.valueOf(calorieHistory.numberOfCalories));
}
@Override
public int getItemCount() {
// Return the size of our array.
return calorieHistoryList.size();
}
public static class HistoryListViewHolder extends RecyclerView.ViewHolder {
public TextView tvDate;
public TextView tvAmount;
public HistoryListViewHolder(View v) {
super(v);
tvDate = (TextView) v.findViewById(R.id.calorie_date);
tvAmount = (TextView) v.findViewById(R.id.calorie_amount);
}
}
}
答案 0 :(得分:7)
根据列表规范,List.add(E object)
应将该项添加到列表的 END 。您似乎在向END添加项目,而在FIRST项目上通知插入。
要将项目添加为第一项,您应该更改
calorieHistories.add(calorieHistoryItem);
到
calorieHistories.add(0, calorieHistoryItem);
修改强>
或者,如果您实际上意味着将项目添加到END,则应更改
historyListAdapter.notifyItemInserted(0);
到
historyListAdapter.notifyItemInserted(calorieHistories.size()-1);