Firebase回收器适配器getKey()仅适用于新数据

时间:2018-07-10 11:18:31

标签: android firebase firebase-realtime-database android-recyclerview

您好,我遇到了当前问题,我的Firebase Recycler Adapter getKey()仅在当前会话中有效,例如,如果您打开该应用程序,则可以使用Firebase Recycler Adapter在Recycler View上插入数据实时数据库中的数据,我可以使用以下代码对其进行编辑并获取密钥:

key_janeiro = (janeiro.getRef(holder.getAdapterPosition()).getKey());

如果我关闭应用程序并销毁它,然后重新打开它,则相同的代码将无法正常工作 getKey();

我正在使用gradle文件中的以下库:
firebase-ui-auth:3.3.1,firebase-database:16.0.1,firebase-ui-database:4.0.1

我不知道可能是问题的原因,如果有人知道如何解决此问题,则删除功能可以正常工作,但是无法从Firebase上已创建的数据中获取密钥。

这是我的firebase数据的打印存储: enter image description here

我的代码是这样的:

        @Override
    protected void onBindViewHolder(final ItemViewHolder holder, final int position, final Item model){

        holder.setDescription(model.getDescription());
        holder.setStatusPagamento(model.getStatus_pagamento());
        holder.setIdfatura(model.getId_fatura());
        holder.txtOptionDigit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {

                key_janeiro = (janeiro.getRef(holder.getAdapterPosition()).getKey());

                PopupMenu popupMenu = new PopupMenu(MesesActivity.this, holder.txtOptionDigit);
                popupMenu.inflate(R.menu.option_menu);
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {


                    @Override
                    public boolean onMenuItemClick(android.view.MenuItem item) {

                        switch (item.getItemId()) {

                            case R.id.editar:

                                DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
                                DatabaseReference faturas = databaseReference.child("Janeiro");
                                DatabaseReference childRef = databaseReference.child(usuario_logado);
                                childRef = databaseReference.child(usuario_logado);

                                childRef.child(ano_Atual).child(mes_Atual).child(key_janeiro).child("description").setValue("editado!");
                                childRef.child(ano_Atual).child(mes_Atual).child(key_janeiro).child("status_pagamento").setValue("Pago");
//                                    holder.setFundoBranco(holder.myBackground);
                                holder.setDescription("Editado !");
                                holder.setValorFatura("R$ 10,00");
                                holder.setFundoVerde(holder.myBackground);
                                recyclerView.getRecycledViewPool().clear();
                                recyclerView.setLayoutManager(new LinearLayoutManager(MesesActivity.this));
                                recyclerView.setAdapter(janeiro);
                                recyclerView.scrollToPosition(holder.getAdapterPosition());
                                notifyDataSetChanged();

                            break;

                            case R.id.menu_item_deletar:
                                //Delete item
                                    //
                                janeiro.getRef(holder.getAdapterPosition());


                                if(janeiro.getItemCount()!=0) {
                                    janeiro.getRef(holder.getAdapterPosition()).removeValue();
                                    janeiro.notifyItemRemoved(holder.getAdapterPosition());
                                }
                                Toast.makeText(MesesActivity.this, "Deletar", Toast.LENGTH_LONG).show();
                                break;
                            default:
                                break;
                        }
                        return false;
                    }
                });
                popupMenu.show();
            }
        });
    }
};

0 个答案:

没有答案