关于使用firebase的RecyclerviewAdapter的问题

问题描述 投票:0回答:1

我的 RecyclerView 适配器遇到问题。当我的 Firebase 数据库中的项目少于 5 个时,适配器可以正常工作,并且点击某个项目会显示正确的详细信息。但是,当我添加更多项目(超过 5 个)时,适配器开始表现不稳定。点击某个项目会显示另一项目的详细信息,而不是我点击的项目的详细信息。我怀疑问题出在我的适配器实现中。谁能帮我找出问题并提出解决方案?

your text

Recyclerview 适配器没有按预期工作,是吗

package com.app.rednet.myspaceapp.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.app.rednet.myspaceapp.Activity.DetailActivity;
import com.app.rednet.myspaceapp.Domain.ItemDomain;
import com.app.rednet.myspaceapp.databinding.ViewholderRecommendedBinding;
import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class RecommendedAdapter extends RecyclerView.Adapter<RecommendedAdapter.Viewholder> {
    private final ArrayList<ItemDomain> items;
    private Context context;
    ViewholderRecommendedBinding binding;

    public RecommendedAdapter(ArrayList<ItemDomain> items) {
        this.items = items;
        this.context = null;
    }

    @NonNull
    @Override
    public RecommendedAdapter.Viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        binding=ViewholderRecommendedBinding.inflate(LayoutInflater.from(viewGroup.getContext()),viewGroup,false);
        context=viewGroup.getContext();
        return new Viewholder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecommendedAdapter.Viewholder viewholder, int i) {
        binding.titleTxt.setText(items.get(i).getTitle());
        binding.priceTxt.setText("MWK"+items.get(i).getPrice());
        binding.addressTxt.setText(items.get(i).getAddress());
        binding.scoreTxt.setText(""+items.get(i).getScore());

        Glide.with(context)
                .load(items.get(i).getPic())
                .into(binding.pic);
        viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewholder.getBindingAdapterPosition();
                if (position != RecyclerView.NO_POSITION) {
                    Intent intent = new Intent(context, DetailActivity.class);
                    ItemDomain item = items.get(position);
                    intent.putExtra("object", item);
                    context.startActivity(intent);
                }
            }
        });

    }

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

    public class Viewholder extends RecyclerView.ViewHolder {
        public Viewholder(ViewholderRecommendedBinding binding) {
            super(binding.getRoot());
        }
    }
}

java android firebase android-recyclerview
1个回答
0
投票

将其添加到你的 onBindViewHolder 中

ItemDomain itemDomain = items.get(position);

然后使用

itemDomain
获取项目的位置

   Intent intent = new Intent(context, DetailActivity.class);
    intent.putExtra("object", itemDomain);
    context.startActivity(intent);

如果 itemDomain 是一个模型,您可能需要确保 ItemDomain 实现 Parcelable 或 Serialized

© www.soinside.com 2019 - 2024. All rights reserved.