You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RecyclerView在用户登录后仅显示单个条目问题排查求助

问题:RecyclerView仅显示单个重复条目,无法展示全部用户上传内容

认证用户登录后,RecyclerView仅能显示单个条目,但该用户实际上传了多张照片,理应展示所有内容。从Firestore获取数据并存储到ArrayList后传入Adapter,但调试发现Adapter中打印的条目存在重复。

MainActivity中Firestore数据获取与ArrayList存储代码

fStore.collection("images").whereEqualTo("userId", Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid()).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if(task.isSuccessful()){
            Item item = new Item();
            itemList = new ArrayList<>();
            for(QueryDocumentSnapshot document: task.getResult()){
                item.setItemName(document.getData().get("name").toString());
                item.setItemPrice(document.getData().get("price").toString());
                item.setItemImage(document.getData().get("image").toString());
                item.setItemDescription(document.getData().get("description").toString());
                itemList.add(item);
                System.out.println("This are my items:"+ itemList.get(0).getItemName());
            }
            shoeAdapter = new ShoeAdapter(MainActivity.this, itemList);
            recyclerView.setAdapter(shoeAdapter);
            shoeAdapter.notifyDataSetChanged();
        }else {
            Log.d(TAG, "Error getting documents: ", task.getException());
        }
    }
});

ShoeAdapter代码

package com.bac.shoesrecyclerview;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

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

    private Context context;
    private List<Item> mItems;

    public ShoeAdapter(Context context, List<Item> items) {
        this.mItems = items;
        this.context = context;
    }

    public void removeItem(int position){
        mItems.remove(position);
        notifyItemRemoved(position);
    }

    public void restoreItem(Item shoes, int position){
        mItems.add(position, shoes);
        notifyItemInserted(position);
    }

    @NonNull
    @Override
    public ShoeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        View contactView = inflater.inflate(R.layout.item_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(contactView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ShoeAdapter.ViewHolder holder, int position) {
         Item upload = mItems.get(position);
        Glide.with(context).load(upload.getItemImage()).into(holder.imageView);
        System.out.println("ITEM ARRAY:"+ upload);

        holder.nameTextView.setText(upload.getItemName());
        holder.nameTextView2.setText(upload.getItemDescription());
        holder.textViewStock.setText(upload.getItemPrice());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView nameTextView;
        public TextView nameTextView2;
        public TextView textViewStock;
        public ImageView imageView;
        public RelativeLayout displayLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
          nameTextView = itemView.findViewById(R.id.textViewDescription);
           nameTextView2 = itemView.findViewById(R.id.textViewName);
          imageView = itemView.findViewById(R.id.imageView);
          textViewStock = itemView.findViewById(R.id.textViewStock);
          displayLayout = itemView.findViewById(R.id.layoutDisplay);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            Item items  = mItems.get(position);
            Intent intent = new Intent(context,display_shoes.class);
            intent.putExtra("name",items.getItemName());
            intent.putExtra("description",items.getItemDescription());
            intent.putExtra("image",items.getItemImage());
            intent.putExtra("price",items.getItemPrice());
            context.startActivity(intent);
        }
    }
}

条目布局(item_layout.xml)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FrameLayout"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/white">

    <RelativeLayout
        android:id="@+id/view_background"
        android:layout_width="match_parent"
        android:layout_height="244dp"
        android:background="#D81B60">

        <ImageView
            android:id="@+id/delete_icon"
            android:layout_width="85dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginStart="50dp"
            android:layout_marginRight="15dp"
            android:src="@drawable/ic_baseline_delete_24" />

        <TextView
            android:layout_width="52dp"
            android:layout_height="165dp"
            android:layout_centerVertical="true"
            android:layout_marginStart="90dp"
            android:layout_marginEnd="-100dp"
            android:layout_toLeftOf="@id/delete_icon"
            android:text="Delete"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold" />

        <RelativeLayout
            android:id="@+id/layoutDisplay"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:padding="10dp">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/roundshape"
                android:scaleType="centerCrop" />

            <TextView
                android:id="@+id/textViewName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/imageView"
                android:ellipsize="end"
                android:fontFamily="sans-serif"
                android:maxLines="1"
                android:textColor="@color/black"
                android:textSize="17sp" />

            <TextView
                android:id="@+id/textViewDescription"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textViewName"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/imageView"
                android:textColor="#555353"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/textViewStock"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_alignParentBottom="true"
                android:layout_marginLeft="30dp"
                android:layout_marginBottom="140dp"
                android:layout_toRightOf="@+id/imageView"
                android:textColor="#D81B60"
                android:textStyle="bold" />

        </RelativeLayout>
    </RelativeLayout>
</FrameLayout>

主布局(包含RecyclerView)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="83dp">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/logOutBtn"
            android:layout_width="85dp"
            android:layout_height="45dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/roundcheck"
            app:layout_constraintEnd_toEndOf="@+id/toolbar"
            app:layout_constraintHorizontal_bias="0.049"
            app:layout_constraintStart_toStartOf="@+id/toolbar"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Logout"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginStart="28dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="@+id/logOutBtn"
            app:layout_constraintStart_toEndOf="@+id/logOutBtn"
            app:srcCompat="@drawable/activeperson" />

        <TextView
            android:id="@+id/LoggedIn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="LoggedIn :"
            android:textColor="#F6F2F2"
            android:textSize="14sp"
            app:layout_constraintStart_toEndOf="@+id/imageView6"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="4dp"
        android:backgroundTint="@color/light_pink"
        android:src="@drawable/ic_baseline_add_box_24" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="90dp" />

</RelativeLayout>

问题根源与解决方案

问题出在MainActivity的循环逻辑中:Item item = new Item(); 被定义在循环外部,导致每次循环只是修改同一个Item对象的属性,然后重复添加到列表中。最终ArrayList里存的都是同一个对象的引用,所有条目自然显示相同内容。

修改方法:将Item对象的创建移到循环内部,每次迭代都生成新的Item实例:

if(task.isSuccessful()){
    itemList = new ArrayList<>();
    for(QueryDocumentSnapshot document: task.getResult()){
        // 每次循环创建新的Item对象
        Item item = new Item();
        item.setItemName(document.getData().get("name").toString());
        item.setItemPrice(document.getData().get("price").toString());
        item.setItemImage(document.getData().get("image").toString());
        item.setItemDescription(document.getData().get("description").toString());
        itemList.add(item);
        System.out.println("This are my items:"+ itemList.get(itemList.size()-1).getItemName());
    }
    shoeAdapter = new ShoeAdapter(MainActivity.this, itemList);
    recyclerView.setAdapter(shoeAdapter);
    shoeAdapter.notifyDataSetChanged();
}

同时建议将日志打印改为itemList.size()-1,这样能看到每个新增条目的名称,确认所有数据都被正确添加。

内容的提问来源于stack exchange,提问作者jayCoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 16:55:21