gpt4 book ai didi

java - E/RecyclerView : No adapter attached; skipping layout while trying to add TextView to layout

转载 作者:行者123 更新时间:2023-12-01 18:11:33 25 4
gpt4 key购买 nike

当适配器为空时,我尝试在新的 TextView 中显示消息,但我不断收到以下错误:E/RecyclerView:未连接适配器;跳过布局。

发生错误的方法:

    @BindView(R.id.courses_recycler_view)
RecyclerView coursesRecyclerView;

private CoursesAdapter coursesAdapter;
private List<CourseEntity> coursesData = new ArrayList<>();
private TextView noCourses;

private void initViewModel() {
final Observer<List<CourseEntity>> coursesObserver = new Observer<List<CourseEntity>>() {
@Override
public void onChanged(List<CourseEntity> courseEntities) {
coursesData.clear();
coursesData.addAll(courseEntities);

if(coursesAdapter == null) {
coursesAdapter = new CoursesAdapter(coursesData, TermDetailsActivity.this);
coursesRecyclerView.setAdapter(termsAdapter);
if (coursesAdapter.getItemCount() == 0) {
noCourses = new TextView(getApplicationContext());
noCourses.setId(R.id.noTerms);
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT);
noCourses.setLayoutParams(lp);
noCourses.setText("No terms found.");
noCourses.setTextColor(Color.parseColor("#000000"));
coursesLayout.addView(noCourses);

ConstraintSet cs = new ConstraintSet();
cs.constrainHeight(noCourses.getId(), ConstraintSet.WRAP_CONTENT);
cs.constrainWidth(noCourses.getId(), ConstraintSet.WRAP_CONTENT);
cs.connect(noCourses.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);
cs.connect(noCourses.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
cs.connect(noCourses.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
cs.connect(noCourses.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
cs.applyTo(coursesLayout);
}
} else {
coursesAdapter.notifyDataSetChanged();
}
}
};
termDetailsViewModel = new ViewModelProvider(this).get(TermDetailsViewModel.class);
termDetailsViewModel.mCourses.observe(this, coursesObserver);

}

我的适配器类:

public class CoursesAdapter extends RecyclerView.Adapter<CoursesAdapter.ViewHolder> {

String pattern = "MM/dd/yyyy";
DateFormat df = new SimpleDateFormat(pattern);

public final List<CourseEntity> mCourses;
private final Context mContext;

public CoursesAdapter(List<CourseEntity> mCourses, Context mContext) {
this.mCourses = mCourses;
this.mContext = mContext;
}


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.courses_list_item, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final CourseEntity course = mCourses.get(position);

String courseStartDate = df.format(course.getCourseStartDate());
String courseEndDate = df.format(course.getCourseEndDate());

holder.courseName.setText(course.getCourseName());
holder.courseStartDate.setText(courseStartDate);
holder.courseEndDate.setText(courseEndDate);

holder.courseListItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

}
});
}

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

public class ViewHolder extends RecyclerView.ViewHolder {

@BindView(R.id.list_course_name)
TextView courseName;
@BindView(R.id.list_course_start_date)
TextView courseStartDate;
@BindView(R.id.list_course_end_date)
TextView courseEndDate;
@BindView(R.id.course_list_layout)
ConstraintLayout courseListItem;

public ViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}

以这种方式添加 TextView,因为这是一个学校项目,部分要求是能够以编程方式添加 View 。

最佳答案

您看到此警告是因为在屏幕上绘制 View 时没有为 RecyclerView 设置适配器。您正在异步加载数据,然后稍后在 RecyclerView 上调用 setAdapter()。因此,当显示 View 时,数据仍在加载,并且您尚未设置适配器。

您所做的很好,但如果您不想收到此警告,您可以向适配器提供一个空列表并在 View 初始化期间设置它。然后,您可以异步更新该列表并调用 notifyDataSetChanged() 来告诉 RecyclerView 自行刷新。

关于java - E/RecyclerView : No adapter attached; skipping layout while trying to add TextView to layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60461402/

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