我无法获得firebase

时间:2017-11-19 09:07:12

标签: android firebase recycler-adapter

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

以前使用firebase适配器 `FirebaseRecyclerAdapter适配器=新的FirebaseRecyclerAdapter(选项){

        @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)。

2 个答案:

答案 0 :(得分:0)

我认为问题在于您使用new

  

com.android.support:recyclerview-v7:

Recyclerview,它有点不同。这是一个tutorial,基本上来自那个课程:
onCreateViewHolder设置ViewHolder,不需要GetRef()如果您不创建自定义<{1}}则不存在

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

答案 1 :(得分:0)

使用此

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

            }
        });