我无法在 Firebase 中获得孩子的钥匙

Posted

技术标签:

【中文标题】我无法在 Firebase 中获得孩子的钥匙【英文标题】:I am unable to get key of a child in firebase 【发布时间】:2018-05-02 16:22:48 【问题描述】:

以前我使用的是 firebase 回收器适配器,我曾经使用 final String NameKey = getRef(position).getKey(); 并使用 putExtraIntent 发送该值并在另一个活动中获取该值。 现在我正在使用自定义回收器适配器,我无法使用此代码来获取键值并在 putExtraIntent 中使用它。我该怎么做?

以前使用 Firebase 适配器 ` FirebaseRecyclerAdapter 适配器 = new FirebaseRecyclerAdapter(options)

        @Override
        public NameHolder onCreateViewHolder(ViewGroup parent, int viewType) 
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.author_list_cv, parent, false);
            return new NameHolder(view);

        

        @Override
        protected void onBindViewHolder(NameHolder holder, int position, AuthorsCLA model) 

            final String AuthorNameKey = getRef(position).getKey();

            holder.name.setText(model.getAuthorname());

            holder.name.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View view) 

                    Intent quotesListIntent = new Intent(AuthorsList.this, AuthorQuotesDetailActvity.class);
                    quotesListIntent.putExtra("nameID", AuthorNameKey);
                    startActivity(quotesListIntent);

                
            ); ` 

现在我正在尝试这个

 @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) 
    final AuthorsCLA authorsModel = authorlist.get(holder.getAdapterPosition());
    final String AuthorNameKey = getRef(position).getKey();

    switch (holder.getItemViewType()) 
        case A: 


            NamesHolder nameHolder = (NamesHolder) holder;
            nameHolder.name.setText(authorsModel.getAuthorname());
            nameHolder.name.setOnClickListener(new View.OnClickListener() 



                @Override
                public void onClick(View view) 
                    Intent intent = new Intent(view.getContext(), AuthorQuotesDetailActvity.class);
                    intent.putExtra("nameID",AuthorNameKey );
                    mcontext.startActivity(intent);

                    Toast.makeText(view.getContext(), authorsModel.getAuthorname(), Toast.LENGTH_SHORT).show();

                

            );

但 getRef 显示错误,当我将鼠标悬停在它上面时,它说无法解析方法 getRef(int)。

【问题讨论】:

如果您向我们展示不起作用的代码,也许有人可以帮助您 对此感到抱歉..我现在更新了我的问题 也许这个答案可以帮助你听起来像problem 【参考方案1】:

我认为问题在于您使用new

com.android.support:recyclerview-v7:

Recyclerview 和它的工作方式有点不同。这是tutorial,基本上来自那节课: (onCreateViewHolder 正在设置ViewHolder,不需要GetRef(),但是如果您不创建自定义,则不存在

    public class Recycler_View_Adapter extends RecyclerView.Adapter<View_Holder> 

    List<Data> list = Collections.emptyList();
    Context context;

    public Recycler_View_Adapter(List<Data> list, Context context) 
        this.list = list;
        this.context = context;
    

    @Override
    public View_Holder onCreateViewHolder(ViewGroup parent, int viewType) 
        //Inflate the layout, initialize the View Holder
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
        View_Holder holder = new View_Holder(v);





    holder .itemView.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            int position = holder .getAdapterPosition();
            // This is org.greenrobot.eventbus
            Application.getInstance().getEventBus().post(new OnHistoryClickEvent(position));
        
    );
    holder .itemView.setOnLongClickListener(new View.OnLongClickListener() 
        @Override
        public boolean onLongClick(View v) 
            int position = holder .getAdapterPosition();
            // This is org.greenrobot.eventbus
            Application.getInstance().getEventBus().post(new OnHistoryLongClickEvent(position));
            return true;
        
    );


    return holder;
    

    @Override
    public void onBindViewHolder(View_Holder holder, int position) 

        //Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView
        holder.title.setText(list.get(position).title);
        holder.description.setText(list.get(position).description);
        holder.imageView.setImageResource(list.get(position).imageId);

        //animate(holder);

    

    @Override
    public int getItemCount() 
        //returns the number of elements the RecyclerView will display
        return list.size();
    

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) 
        super.onAttachedToRecyclerView(recyclerView);
    

    // Insert a new item to the RecyclerView on a predefined position
    public void insert(int position, Data data) 
        list.add(position, data);
        notifyItemInserted(position);
    

    // Remove a RecyclerView item containing a specified Data object
    public void remove(Data data) 
        int position = list.indexOf(data);
        list.remove(position);
        notifyItemRemoved(position);
    

View_Holder

public class View_Holder extends RecyclerView.ViewHolder 

   CardView cv;
   TextView title;
   TextView description;
   ImageView imageView;

   View_Holder(View itemView) 
       super(itemView);
       cv = (CardView) itemView.findViewById(R.id.cardView);
       title = (TextView) itemView.findViewById(R.id.title);
       description = (TextView) itemView.findViewById(R.id.description);
       imageView = (ImageView) itemView.findViewById(R.id.imageView);
   

【讨论】:

在哪里添加 onClickListener ? onCreateViewHolder() 归还持有人。从那里您可以将监听器附加到onCreateViewHolder()onBindViewHolder() 甚至在支架中。我更新我的答案.. 如果这回答了您的问题,请将其标记为已回答【参考方案2】:

使用这个

nameHolder.name.setTag(position);    
nameHolder.name.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                int position = (int) view.getTag();
                String AuthorNameKey = getRef(position).getKey()
                Intent quotesListIntent = new Intent(AuthorsList.this, AuthorQuotesDetailActvity.class);
                quotesListIntent.putExtra("nameID", AuthorNameKey);
                startActivity(quotesListIntent);

            
        );

【讨论】:

以上是关于我无法在 Firebase 中获得孩子的钥匙的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Firebase 中获得上孩子?

如何在firebase数据库中获取所有孩子的数据?

Firebase + Android - 通知 ChildAdded - 如何只获得新的孩子?

无法从Firebase获取价值

遍历 Firebase 中一个孩子的所有键

无法构建模块 Firebase