我希望总计显示recyclerView
中显示的项目数。目前,我正在尝试通过TextView
来实现这一目标。
这是我的Adapterclass
@Override
public int getItemCount()
{
final int count = options.getSnapshots() != null
? options.getSnapshots().size()
: 0;
return count;
}
这是我尝试在recyclerview
的实际课堂中实现这一目标的方法:
attendanceNumbers = findViewById(R.id.attendanceNums);
attendanceNumbers.setText(adapter.getItemCount()+ " students have attended this session");
这是我的完整适配器类
public class AttendanceAdapter extends FirestoreRecyclerAdapter<Attendance, AttendanceAdapter.AttendanceHolder>{
private AttendanceAdapter.OnItemClickListener listener;
private FirestoreRecyclerOptions<Attendance> options;
public AttendanceAdapter(@NonNull FirestoreRecyclerOptions<Attendance> options) {
super(options);
this.options = options;
}
@Override
public int getItemCount()
{
final int count = options.getSnapshots() != null
? options.getSnapshots().size()
: 0;
return count;
}
@Override
protected void onBindViewHolder(@NonNull AttendanceHolder holder, int position, @NonNull Attendance model) {
holder.textViewStudent.setText(model.getUserEmail());
holder.textViewTime.setText(model.getSignInTime());
holder.textViewSession.setText(model.getSessionID());
holder.textViewUserUID.setText(model.getUserUID());
}
@NonNull
@Override
public AttendanceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.attendance_item, parent, false);
return new AttendanceAdapter.AttendanceHolder(v);
}
public void deleteItem(int position){
getSnapshots().getSnapshot(position).getReference().delete();
}
public int countItems(){
return getItemCount();
}
class AttendanceHolder extends RecyclerView.ViewHolder {
TextView textViewStudent;
TextView textViewTime;
TextView textViewSession;
TextView textViewUserUID;
public AttendanceHolder(View itemView){
super(itemView);
textViewStudent = itemView.findViewById(R.id.text_view_student_name);
textViewTime = itemView.findViewById(R.id.text_view_course);
textViewSession = itemView.findViewById(R.id.text_view_student_ID);
textViewUserUID = itemView.findViewById(R.id.text_view_user_UID);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION && listener != null){
listener.onItemClick(getSnapshots().get(position),position);
}
}
});
}
}
public interface OnItemClickListener{
void onItemClick(Attendance documentSnapshot, int position);
}
public void setSetOnItemClickListener(AttendanceAdapter.OnItemClickListener listener){
this.listener = listener;
}
这是logcat错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.dissertation.studentscanner.AttendanceAdapter.getItemCount()' on a null object reference
这是我在课堂上创建适配器的地方:不是我的adapterClass
private void setUpRecyclerView() {
final Query query = attendanceRef.whereEqualTo("sessionID", "5I0PLcpgmwdcjKLYLgkU").orderBy("userEmail", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<Attendance> options = new FirestoreRecyclerOptions.Builder<Attendance>()
.setQuery(query, Attendance.class).build();
adapter = new AttendanceAdapter(options);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
attendanceNumbers = findViewById(R.id.attendanceNums);
attendanceNumbers.setText((adapter.getItemCount())+ " students have attended this session");
答案 0 :(得分:1)
NullPointerException
是由于您尝试调用
attendanceNumbers.setText(adapter.getItemCount()+ " students have attended this session");
在创建adapter
变量之前。
创建适配器后移动上述语句即可解决问题。
还要确保
attendanceNumbers = findViewById(R.id.attendanceNums);
在设置attendanceNumbers
文本之前调用。
答案 1 :(得分:0)
需求看起来像您想观察
1)添加学生
2)删除学生等。
为此,创建如下所示的回调
public interface CountObserver {
void onStudentsCount(int count);
}
更改Adapter类,以传递回调
CountObserver countObserver;
public AttendanceAdapter(@NonNull FirestoreRecyclerOptions<Attendance> options,CountObserver listener) {
super(options);
this.options = options;
this.countObserver = listener;
}
@Override
public int getItemCount()
{
final int count = options.getSnapshots() != null
? options.getSnapshots().size()
: 0;
if(countObserver!=null){
countObserver.onStudentsCount(count);
}
return count;
}
最后一步
CountObserver countObserver = new CountObserver(){
public void onStudentsCount(int count){
attendanceNumbers.setText(count+ " students have attended this session");
}
}
adapter = new AttendanceAdapter(options,countObserver);
对于NPE,只需确保在setUpRecyclerView
之后调用setContentView
方法,并在活动xml布局中定义了ID为attendanceNums
的 textView android:id="@+id/attendanceNums"