我的Recyclerview
存在问题,我无法找到解决方法。实际上,我的Recyclerview
应该显示从外部数据库接收的数据。但有时,它会显示数据,有时则不会。
这是我应用的屏幕截图:
这是我的适配器代码:
public class UpcomingLessonAdapter extends RecyclerView.Adapter<UpcomingLessonAdapter.ViewHolder> {
private List<Lesson> lessons;
private LayoutInflater inflater;
public UpcomingLessonAdapter(List<Lesson> lessons, Context context) {
this.lessons = lessons;
inflater = LayoutInflater.from(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = inflater.inflate(R.layout.upcoming_lesson_list_view, parent, false);
itemView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
return new UpcomingLessonAdapter.ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Lesson lesson = lessons.get(position);
String lessonTopic = "<font color='#E166E1'>" + lesson.getTopicTitle() + "</font>";
String userName = "<font color='#E166E1'>" + lesson.getUserName() + "</font>";
holder.lessonTopic.setText(Html.fromHtml(lesson.getTime(lesson.getTimeStart()) + " " + lessonTopic));
holder.lessonTeacher.setText(Html.fromHtml("avec " + userName));
holder.lessonDuration.setText(lesson.getDuration());
holder.lessonDay.setText(lesson.getDay(lesson.getTimeStart()));
holder.lessonMonth.setText(lesson.getMonth(lesson.getTimeStart()));
}
@Override
public int getItemCount() {
return lessons.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
TextView lessonTopic, lessonTeacher, lessonDuration, lessonDay, lessonMonth;
public ViewHolder(View itemView) {
super(itemView);
lessonTopic = (TextView) itemView.findViewById(R.id.lesson_topic_text_view);
lessonTeacher = (TextView) itemView.findViewById(R.id.lesson_teacher_text_view);
lessonDuration = (TextView) itemView.findViewById(R.id.lesson_duration_text_view);
lessonDay = (TextView) itemView.findViewById(R.id.lessson_day_text_view);
lessonMonth = (TextView) itemView.findViewById(R.id.lesson_month_text_view);
}
}
}
这是我创建recyclelerview时片段中的方法:
public void displayUpcomingLessonListView() {
upcomingLessonRecyclerView = (RecyclerView) view.findViewById(R.id.upcoming_lesson);
upcomingLessonAdapter = new UpcomingLessonAdapter(upcomingLessons, getContext());
upcomingLessonLayoutManager = new LinearLayoutManager(getContext());
upcomingLessonRecyclerView.setLayoutManager(upcomingLessonLayoutManager);
upcomingLessonRecyclerView.setItemAnimator(new DefaultItemAnimator());
upcomingLessonRecyclerView.setAdapter(upcomingLessonAdapter);
}
有没有人可以给我一些建议?提前谢谢。
致以最诚挚的问候,
修改
这是我获取数据的方法:
public void getLessonInfos(final int lessonId, final int index, final List<Lesson> lessons) {
Call<JsonResponse> call = service.getLessonInfos(lessonId, user.getEmail(), user.getToken());
call.enqueue(new Callback<JsonResponse>() {
@Override
public void onResponse(Call<JsonResponse> call, Response<JsonResponse> response) {
lessons.get(index).setUserName(response.body().getUserName());
lessons.get(index).setTopicTitle(response.body().getTopicTitle());
lessons.get(index).setTopicGroupTitle(response.body().getTopicGroupTitle());
lessons.get(index).setLevel(response.body().getLevelTitle());
lessons.get(index).setDuration(response.body().getDuration().getHours(), response.body().getDuration().getMinutes());
lessons.get(index).setStatus(response.body().getLessonStatus());
if (upcomingLessons.size() > 0) {
if (lessonId == upcomingLessons.get(upcomingLessons.size() - 1).getLessonId()) {
displayUpcomingLessonListView();
}
}
}
@Override
public void onFailure(Call<JsonResponse> call, Throwable t) {
}
});
}
答案 0 :(得分:0)
try {
ResponseBody response = call.execute().body();
} catch (IOException e) {
e.printStackTrace();
}
当您异步调用方法时,网络会发出请求,然后等待响应。延迟或更确切地说,时间将根据您的连接速度(每秒字节数)而不是您的数据集(大量数据,需要时间来处理)而变化。因此,您的代码有时可行的原因是因为有时请求和响应时间足够快以及时更新您的数据。上面的代码段删除了上述方法,因此离开了等待响应的概念。它同步发出请求并立即获得响应。没有等待时间,唯一等待的时间是从存储源中获取数据的时间,处理这些数据,转换并发送它。
答案 1 :(得分:0)
使用三元运算符
holder.lessonDuration.setText(lesson.getDuration()!=null?lesson.getDuration():"");