为什么我的Firebase中的Strings不会在myRecyclerView上显示

时间:2018-03-28 17:37:56

标签: android firebase

documentation

我的数据库是这样的,我已经在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检索它以及我能找到的任何其他内容

  

更新我的适配器

    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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用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();
                }
            }