我想从Firebase获取产品ID

时间:2019-07-17 21:56:21

标签: java firebase

我正在从Firebase检索产品信息,但是问题是我无法选择Firebase中存储的任何特定ID。

我针对包括get(position)在内的问题尝试了不同的解决方案,但是它正在RecyclerView中获得产品的位置

这是ProductsAdapter onBindViewHolder函数。

使用*来添加更改

    @Override
    public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
        Product productCurrent = mProducts.get(position);
        holder.product_title.setText(toCapitalize(productCurrent.getpTitle()));
        holder.product_pice.setText("$: " + productCurrent.getpPrice());
        Picasso.with(mContext)
                .load(productCurrent.getpImageUrl())
                .placeholder(R.mipmap.ic_launcher)
                .fit()
                .centerCrop()
                .into(holder.product_image);

        holder.parentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ProductPreview.class);
                intent.putExtra("pImageUrl", productCurrent.getpImageUrl());
                intent.putExtra("pTitle", toCapitalize(productCurrent.getpTitle()));
                intent.putExtra("pPrice", productCurrent.getpPrice());
                intent.putExtra("pDescription", toCapitalize(productCurrent.getpDescription()));

                Toast.makeText(mContext, " " + position, Toast.LENGTH_SHORT).show();

                mContext.startActivity(intent);

            }
        });
    }

此代码来自具有RecyclerView的ProductsActivity

    private ProductsAdapter mAdapter;

    private ProgressBar mProgressBar;

    DatabaseReference mDatabaseReference;
    private ArrayList<Product> mProducts;
    ***ArrayList<ProductKeys> mProductKeys;***

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_products);

        mRecyclerView = findViewById(R.id.products_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),2);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);

        mProducts = new ArrayList<>();
        ***mProductKeys = new ArrayList<>();***

        mDatabaseReference = FirebaseDatabase.getInstance().getReference("products");

        mDatabaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot postSnapShot : dataSnapshot.getChildren())
                {
                    Product product = postSnapShot.getValue(Product.class);
                    mProducts.add(product);
                    ***mProductKeys.add(postSnapShot.getKey());***
                }
                mAdapter = new ProductsAdapter(ProductsActivity.this, mProducts);
                mRecyclerView.setAdapter(mAdapter);
                mProgressBar.setVisibility(View.INVISIBLE);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(ProductsActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
                mProgressBar.setVisibility(View.INVISIBLE);
            }
        });

获取错误:

(com.*)
in ArrayList cannot be applied
to
(java.lang.String)

请帮助我...在此先感谢

2 个答案:

答案 0 :(得分:0)

如果您尝试通过position方法在onBindViewHolder处确定该项目的Firebase数据库ID,则必须自己维护该映射。您可以通过从getKey()的快照中提取getValue()onDataChange并保留两者的列表来轻松地做到这一点。

类似这样的东西:

mProductKeys = new ArrayList<String>();

mDatabaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot postSnapShot : dataSnapshot.getChildren())
        {
            Product product = postSnapShot.getValue(Product.class);
            mProducts.add(product);
            mProductKeys.add(postSnapShot.getKey());
        }
        mAdapter = new ProductsAdapter(ProductsActivity.this, mProducts);
        mRecyclerView.setAdapter(mAdapter);
        mProgressBar.setVisibility(View.INVISIBLE);
    }

然后在onBindViewHolder中,您可以查找在mProductKeys中单击的项目的键。

答案 1 :(得分:0)

好吧,我尝试了不同的方法,@ Frank的回答给了我一些想法。只需添加一条线并完成。 :)无需添加ArrayList<String>

    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot postSnapShot : dataSnapshot.getChildren())
        {
            Product product = postSnapShot.getValue(Product.class);
            product.setpKey(postSnapShot.getKey());
            mProducts.add(product);
        }
        mAdapter = new ProductsAdapter(ProductsActivity.this, mProducts);
        mRecyclerView.setAdapter(mAdapter);
        mProgressBar.setVisibility(View.INVISIBLE);
    }

并将String pKeyProduct.class添加到我的Setter and Getter中。