【问题标题】:How to change my list data according to selected date?如何根据所选日期更改我的列表数据?
【发布时间】:2019-01-11 21:22:06
【问题描述】:

在片段中,我调用 diaryViewModel 并在 onActivityCreated() 中使用日期参数:

        mDiaryViewModel = ViewModelProviders.of(this, new MyViewModelFactory(this.getActivity().getApplication(), date)).get(DiaryViewModel.class);

另外,我在布局(片段)顶部有一个文本视图,当前日期位于两个箭头之间。左箭头表示单击它时,当前日期将变为负一天,另一个箭头变为正一天。根据更改的日期,我希望实时数据(回收站视图)也发生更改。这是调用 onActivityCreated() 的代码,我在箭头的 onClick 方法中插入了相同的代码。

  mDiaryViewModel = ViewModelProviders.of(this, new MyViewModelFactory(this.getActivity().getApplication(), date)).get(DiaryViewModel.class);

    mDiaryViewModel.getTodayEntries(date).observe(this, new Observer<List<Diary>>() {
        @Override
        public void onChanged(@Nullable final List<Diary> diary) {
            // Update the cached copy of the words in the adapter.
            adapter.setDiary(diary);
        }
    });

适配器代码:

public class DiaryListAdapter extends RecyclerView.Adapter<DiaryListAdapter.DiaryViewHolder> {

class DiaryViewHolder extends RecyclerView.ViewHolder {
    private final TextView timeItemView;
    private final TextView foodNameItemView;
    private final TextView gramsItemView;

    private DiaryViewHolder(View itemView) {
        super(itemView);
        timeItemView = itemView.findViewById(R.id.time);
        foodNameItemView = itemView.findViewById(R.id.f_name);
        gramsItemView = itemView.findViewById(R.id.f_grams);

    }
}

private final LayoutInflater mInflater;
private List<Diary> mDiary; // Cached copy of user

DiaryListAdapter(Context context) { mInflater = LayoutInflater.from(context); }


@Override
public DiaryListAdapter.DiaryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = mInflater.inflate(R.layout.recyclerview_diary, parent, false);
    return new DiaryListAdapter.DiaryViewHolder(itemView);
}

@Override
public void onBindViewHolder(DiaryListAdapter.DiaryViewHolder holder, int position) {
    if (mDiary != null) {
        Diary current = mDiary.get(position);
        holder.timeItemView.setText(String.valueOf(current.getTime()));
        holder.foodNameItemView.setText(current.getFoodName());
        holder.gramsItemView.setText(String.valueOf(current.getGrams()));
    } else {
        // Covers the case of data not being ready yet.
        holder.foodNameItemView.setText("No User");
    }
}

void setDiary(List<Diary> diaries){
    mDiary = diaries;
    notifyDataSetChanged();
}

// getItemCount() is called many times, and when it is first called,
// mWords has not been updated (means initially, it's null, and we can't return null).
@Override
public int getItemCount() {
    if (mDiary != null)
        return mDiary.size();
    else return 0;
}

}

这里的屏幕原型可以为您提供更多帮助: prototype

【问题讨论】:

    标签: java android android-studio listview android-fragments


    【解决方案1】:

    更新数据后,调用adapter.notifyDataSetChanged()方法。这将更新数据

    mDiaryViewModel = ViewModelProviders.of(this, new MyViewModelFactory(this.getActivity().getApplication(), date)).get(DiaryViewModel.class);
    
    mDiaryViewModel.getTodayEntries(date).observe(this, new Observer<List<Diary>>() {
        @Override
        public void onChanged(@Nullable final List<Diary> diary) {
            // Update the cached copy of the words in the adapter.
            adapter.setDiary(diary);
            // This will refresh the data 
            **adapter.notifyDatasetChanged()**
        }
    });
    

    如果刷新数据不是问题,请告诉我

    【讨论】:

    • 点击后数据仍然没有变化。
    • 您确定在 ibserver 中收到了数据吗?
    • 请在onchaged方法中输入一个log in,看看它是否达到了那个点
    • 是的,它正在到达它。
    • 能否贴出你的适配器代码,调试起来很方便
    猜你喜欢
    • 2016-04-10
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 2022-11-28
    • 2015-04-15
    • 1970-01-01
    • 2023-01-10
    相关资源
    最近更新 更多