我目前正在使用Fragment类,它从其他类中获取String值。 我需要将这些值填充到适配器类中并设置视图。学生到达时间和学生姓名的视图类数据由服务器获取 -
Student arrival time Student name Present/Absent
10:50 John
10:55 Alex
11:00 Peter
11:07 Mark
我在Fragment类中获取Present / Absent的String值,我需要在适配器类中更新Present / Absent列中的视图。 请注意,到达时间按升序排列。因此,当呼叫出席时,通过语音识别识别出现/缺席 填写在当前/缺席列中。
我有两个问题 -
a)我需要为列学生姓名和学生到达时间
创建完全相同行数的文本视图b)即使我为完全相同的行数创建了文本视图,如何每次都将textview值更改为“是”或“否”,因为更改textview一次会因其余部分而改变
答案 0 :(得分:0)
我认为你应该使用RecyclerView
Here你可以找到一个很好的描述如何使用RecyclerView
以下是我的示例:它应该是什么样的:
public class YourFragment extends Fragment {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private List<Student> studentList;
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
studentList = new ArrayList<>();
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
adapter = new StudentsAdapter(studentList);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
//This method fetch students list from server and insert them into studentList
fetchStudentsList();
}
}
public class StudentsAdapter extends RecyclerView.Adapter<StudentsAdapter.ViewHolder> {
private List<Student> studentList;
public StudentsAdapter(List<Student> studentList) {
this.studentList = studentList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.student_layout, parent, false);
ViewHolder holder = new ViewHolder(itemView);
holder.name = (TextView) itemView.findViewById(R.id.student_name);
holder.arrivalTime = (TextView) itemView.findViewById(R.id.student_arrival_time);
holder.present = (TextView) itemView.findViewById(R.id.student_present);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Student student = studentList.get(position);
holder.name.setText(student.name);
holder.arrivalTime.setText(student.arrivalTime);
holder.present.setText(student.present ? R.string.yes : R.string.no);
}
@Override
public int getItemCount() {
return studentList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView arrivalTime;
public TextView present;
public ViewHolder(View itemView) {
super(itemView);
}
}
}
public class Student {
public String arrivalTime;
public String name;
public boolean present;
}
要更新recyclerView中的某些信息,只需更改Student
对象中的值,然后调用方法adapter.notifyDataSetChanged()
另见: