gpt4 book ai didi

android - 为什么我的 Firebase 字符串不会显示在 myRecyclerView 上

转载 作者:行者123 更新时间:2023-11-30 00:03:01 26 4
gpt4 key购买 nike

enter image description here

我的数据库是这样的,我在 push() 键下添加了字符串。问题是当我将它添加到我的 ArrayList 时,它在我的 RecyclerView 中没有显示任何内容。

这就是我将字符串输入数据库的方式,

mToFreezer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences prefs = getActivity().getSharedPreferences("itemInfo", Context.MODE_PRIVATE);
String name = prefs.getString("Name1", null);
String date = prefs.getString("Date1", null);


HashMap<String, String> dataMap = new HashMap<>();
dataMap.put("Name", name);
dataMap.put("Date", date);

mDatabase.push().setValue(dataMap);

然后我在另一个 Activity 中检索了它,

public RecyclerView mRecyclerView;
public RecyclerView.LayoutManager mLayoutManager;
public RecyclerView.Adapter mAdapter;
public ArrayList<Event> mDataSet;
public Button AddFreezerItems;
private DatabaseReference mDatabase;

// OnCreate

mDataSet = new ArrayList<>();
mDatabase = FirebaseDatabase.getInstance().getReference().child("FreezerItems");
String mGroupId = mDatabase.push().getKey();
mDatabase.keepSynced(true);

mRecyclerView = findViewById(R.id.FreezerInventoryRecyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new ItemRecyclerViewAdapter(mDataSet);
mRecyclerView.setAdapter(mAdapter);

AddFreezerItems = findViewById(R.id.addItemsFreezer);


mDatabase.child(mGroupId).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {

for(DataSnapshot child : dataSnapshot.getChildren()) {
HashMap<String, String> value = (HashMap<String,String>) child.getValue();
if (value != null) {
String name = value.get("name");
String date = value.get("date");
mDataSet.add(new Event(name,date,null));
mAdapter.notifyDataSetChanged();
}
}

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

我已经尝试了所有方法,从将数据作为 ArrayList 放入 Firebase 并使用 GenericTypeIndicator 检索它,以及我能找到的任何其他方法

UPDATE My Adapter

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

private ArrayList<Event> mDataSet;

public ItemRecyclerViewAdapter(ArrayList<Event> mDataset){
this.mDataSet = mDataset;
}

@Override
public ItemRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_view_row_inventory, parent, false);

ViewHolder vh = new ViewHolder(v);
return vh;
}

@Override
public void onBindViewHolder(ItemRecyclerViewAdapter.ViewHolder holder, int position) {
Event product = mDataSet.get(position);
holder.mItemName.setText(product.getName());
holder.mItemDate.setText(product.getDate());

}

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

public class ViewHolder extends RecyclerView.ViewHolder {

private TextView mItemName;
private TextView mItemDate;

private ViewHolder(View itemView) {
super(itemView);
mItemName = itemView.findViewById(R.id.ItemNameTxtView);
mItemDate = itemView.findViewById(R.id.expirationDateTxtView);
}
}
}

最佳答案

尝试从你的 onCreate 方法中删除它:

String mGroupId = mDatabase.push().getKey();

然后像这样调用你的事件监听器:

mDatabase.addChildEventListener(new ChildEventListener() {

我想你听错了位置。您的数据库已经指向 FreezerItems 列表:

mDatabase = FirebaseDatabase.getInstance().getReference().child("FreezerItems");

当您将 child(mGroupId) 添加到引用时,您指向的是一个不存在的引用,因为刚刚为您生成了 mGroupId

编辑:

当您执行此 dataSnapshot.getChildren() 时,您将获得最近添加的对象的字段:

       public void onChildAdded(DataSnapshot dataSnapshot, String s) {

for(DataSnapshot child : dataSnapshot.getChildren()) {
HashMap<String, String> value = (HashMap<String,String>) child.getValue();
if (value != null) {
String name = value.get("name");
String date = value.get("date");
mDataSet.add(new Event(name,date,null));
mAdapter.notifyDataSetChanged();
}
}

}

尝试这样做:

public void onChildAdded(DataSnapshot dataSnapshot, String s) {

HashMap<String, String> value = (HashMap<String,String>) dataSnapshot.getValue();
if (value != null) {
String name = value.get("name");
String date = value.get("date");
mDataSet.add(new Event(name,date,null));
mAdapter.notifyDataSetChanged();
}
}

关于android - 为什么我的 Firebase 字符串不会显示在 myRecyclerView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49540884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com