计算 RecyclerView 显示的项数并放入一个 TextView

目前,我可以在我的RecyclerView. 我想要做的是计算这些项目中有多少。我的项目保存在CardView. 我不想计算或求和任何特定字段,只想计算实际项目的数量。这是我的代码,我尝试使用getItemCount()方法设置它并将其附加到 TextView 并在 RecyclerView 之外显示。这对我不起作用:


public class COM800s1 extends AppCompatActivity {


private FirebaseFirestore db = FirebaseFirestore.getInstance();

private CollectionReference attendanceRef = db.collection("Attendance");

private AttendanceAdapter adapter;

private Context context;

private TextView attendanceNumbers;

private int counter;



@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_com800s1);


    attendanceNumbers = findViewById(R.id.attendanceNums);


    FloatingActionButton floatingActionButton = findViewById(R.id.button_add_session);

    floatingActionButton.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            startActivity(new Intent(COM800s1.this,NewAttendanceRecord.class));

        }

    });


    setUpRecyclerView();



}


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);


   //counter = adapter.getItemCount();

   //attendanceNumbers.setText("Currently " +counter+ " students have attended this session");



    new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,

            ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

        @Override

        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {

            return false;

        }


繁星coding
浏览 190回答 3
3回答

神不在的星期二

如果您试图将counter变量作为全局变量并在回调之外使用它,那将始终是null因为正如@Vinnie 在他的回答中提到的那样,数据尚未完成加载。真正发生的事情是您试图从数据库中获取数据并立即使用它,而counter变量的初始值为0.&nbsp;然后,当从数据库中获取数据完成时,它会更改计数器的值,但不会再次读取。为了解决这个问题,getItemCount()在你的适配器类中使用你的方法的结果将它的值设置为TextView.

慕森卡

如果您想获取您的 recyclerView 列表,只需在您的 recyclerView 适配器中创建一个静态方法,然后在其中返回您的列表。例如:public static List<modelOfYourList> getCount(){&nbsp; &nbsp;return yourAdapterList;}

犯罪嫌疑人X

也许在你的AttendanceAdapter课堂上添加这样的东西?我看了一下FirestoreRecyclerOptions类,它公开了一个getSnapshots()方法。这将返回 a ObservableSnapshotArray,而后者又从 扩展而来Base ObservableSnapshotArray,它公开了一个size()方法。class AttendanceAdapter extends FirestoreRecyclerAdapter<Attendance, AttendanceAdapter.AttendanceHolder>{{&nbsp;&nbsp; &nbsp; private FirestoreRecyclerOptions<Attendance> options;&nbsp; &nbsp; public AttendanceAdapter(@NonNull FirestoreRecyclerOptions<Attendance> options)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; super(options);&nbsp; &nbsp; &nbsp; &nbsp; this.options = options;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getItemCount()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; final int count = options.getSnapshots() != null&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? options.getSnapshots().size()&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : 0;&nbsp; &nbsp; &nbsp; &nbsp; return count;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java