如何将子firebase数据库中的特定名称获取到recyclerview?
Posted
技术标签:
【中文标题】如何将子firebase数据库中的特定名称获取到recyclerview?【英文标题】:How to get specific name inside child firebase database into recyclerview? 【发布时间】:2021-10-08 02:43:01 【问题描述】:我有点困惑,如何在子键中获取特定名称,我只知道 set recyclerview,但不知道如何将 grandprice 设置为 TextView,因为 grandprice 在另一个子键中,我的数据库在下面。
key 是 CardView,grandprice 是 TextView。
这是我的代码
//RecyclerView
mRecyclerView = view.findViewById(R.id.recommeneded);
mRecyclerView.setHasFixedSize(true);
//mRecyclerView.setLayoutManager(new LinearLayoutManager(HomeActivity.this));
final RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 1);
mRecyclerView.setLayoutManager(mLayoutManager);
mProgressBar = view.findViewById(R.id.myDataLoaderProgressBar);
mProgressBar.setVisibility(View.VISIBLE);
final int position = 0;
recommended = new ArrayList<>();
mAdapter = new InvoiceAdapter(getActivity(), recommended);
mRecyclerView.setAdapter(mAdapter);
//mAdapter.setOnItemClickListener(HomeActivity.this);
rootRef = FirebaseDatabase.getInstance().getReference();
usersRef = rootRef.child("order");
uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
mDBListener = usersRef.child(uid).addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
recommended.clear();
for (DataSnapshot invoiceSnapshot : dataSnapshot.getChildren())
InvoiceModels upload = invoiceSnapshot.getValue(InvoiceModels.class);
upload.setKey(invoiceSnapshot.getKey());
recommended.add(position, upload);
mAdapter.notifyDataSetChanged();
mProgressBar.setVisibility(View.GONE);
@Override
public void onCancelled(DatabaseError databaseError)
Toast.makeText(getActivity(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
mProgressBar.setVisibility(View.INVISIBLE);
);
//RecyclerView
我的适配器
@Override
public void onBindViewHolder(RecyclerViewHolder holder, final int position)
InvoiceModels currentProduct = recomended.get(position);
//holder.totalPrice.setText(currentProduct.getTotalPrice());
Toast.makeText(mContext, currentProduct.getTotalPrice(), Toast.LENGTH_SHORT).show();
@Override
public int getItemCount()
return recomended.size();
public class RecyclerViewHolder extends RecyclerView.ViewHolder
TextView totalPrice;
public RelativeLayout relativelayout;
public RecyclerViewHolder(View itemView)
super(itemView);
totalPrice = itemView.findViewById(R.id.totalPrice);
relativelayout = (RelativeLayout) itemView.findViewById(R.id.relativeLayout);
衷心感谢您的帮助, 阿尔。
【问题讨论】:
问题解决了吗? 不,还没有!但是当我解决问题时我会检查/评论,谢谢 好的,让我知道。 【参考方案1】:首先,您需要获取 inv-details
的 id
。接下来就可以调用里面的数据了。你可以试试这个。
recommended.clear();
//At here you get the id first.
final String keyOfChild = dataSnapshot.getKey();
usersRef.child(uid).child(keyOfChild).child("inv-details").addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
InvoiceModels upload = dataSnapshot.getValue(InvoiceModels.class);
upload.setKey(invoiceSnapshot.getKey());
recommended.add(position, upload);
mAdapter.notifyDataSetChanged();
mProgressBar.setVisibility(View.GONE);
【讨论】:
以上是关于如何将子firebase数据库中的特定名称获取到recyclerview?的主要内容,如果未能解决你的问题,请参考以下文章
将子值从 firebase 获取到 android 应用程序
如何在 Android Studio 中使用 Kotlin 从 Firebase 中的数据库中获取特定值?