第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

我想從 Firebase 獲取產(chǎn)品 ID

我想從 Firebase 獲取產(chǎn)品 ID

富國(guó)滬深 2023-03-31 09:58:27
我正在從 Firebase 檢索產(chǎn)品信息,但問(wèn)題是我無(wú)法選擇存儲(chǔ)在 Firebase 中的任何特定 ID。我嘗試了不同的解決方案來(lái)解決這個(gè)問(wèn)題,get(position)但它正在獲取產(chǎn)品在 RecyclerView 中的位置這是 ProductsAdapteronBindViewHolder函數(shù)。使用 * 添加更改    @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);            }        });    }此代碼來(lái)自 ProductsActivity 具有RecyclerView    private ProductsAdapter mAdapter;    private ProgressBar mProgressBar;    DatabaseReference mDatabaseReference;    private ArrayList<Product> mProducts;    ***ArrayList<ProductKeys> mProductKeys;***
查看完整描述

2 回答

?
心有法竹

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊

如果您嘗試在position方法中確定項(xiàng)目的 Firebase 數(shù)據(jù)庫(kù) ID onBindViewHolder,則必須自己維護(hù)該映射。您可以通過(guò)從快照中提取getKey()和并保留它們的列表來(lái)輕松地做到這一點(diǎn)。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您可以查找在 中單擊的項(xiàng)目的鍵mProductKeys。


查看完整回答
反對(duì) 回復(fù) 2023-03-31
?
人到中年有點(diǎn)甜

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊

好的,我嘗試了不同的方法,只需添加一行即可完成。:) 無(wú)需添加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 pKey到我的Product.classwith Setter and Getter。


查看完整回答
反對(duì) 回復(fù) 2023-03-31
  • 2 回答
  • 0 關(guān)注
  • 190 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)