如何在android中使用recyclelerview从一个适配器获取价值到另一个适配器?

时间:2018-02-07 11:53:20

标签: android-recyclerview

如何在单击收藏夹按钮时从一个适配器获取值到另一个适配器,然后显示我们点击收藏图像按钮的数量,并显示另一个活动中的所有数据。以下是我的Adapter Class& ViewHolder类(即 AdapterFav.java & ContactViewHolder.java

AdapterFav.java

    public class AdapterFav extends 
        RecyclerView.Adapter<AdapterFav.ContactViewHolder>{

        private ArrayList<Contactfav> contactfavList;
        private static Context mContext;

        public AdapterFav(ArrayList<Contactfav> contactfavList, Context applicationContext) {
            this.contactfavList = contactfavList;
            this.mContext = applicationContext;
        }

        @Override
        public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.contact_row, null);
            ContactViewHolder contactViewHolder = new ContactViewHolder(view);
            return contactViewHolder;
        }

        @Override
        public void onBindViewHolder(ContactViewHolder holder, int position) {
            Contactfav contactfav = (Contactfav) contactfavList.get(position);
            holder.tvContactName.setText(contactfav.getContactName());
            holder.tvPhoneNumber.setText(contactfav.getContactNumber());
            //Log.e("Id is", contactfav.getContactNumber());
            holder.ivContactImage.setImageBitmap(contactfav.getContactImage());
            holder.id=contactfav.getCid();
        }

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

ContactViewHolder.java

 public static class ContactViewHolder extends RecyclerView.ViewHolder {

        ImageView ivContactImage;
        ImageButton ibFavBtn;
        TextView tvContactName;
        TextView tvPhoneNumber;
        Contactfav contactfav=new Contactfav();
        String id;
        public ContactViewHolder(View itemView) {
            super(itemView);
            ivContactImage = (ImageView) itemView.findViewById(R.id.ivContactImage);
            tvContactName = (TextView) itemView.findViewById(R.id.tvContactName);
            tvPhoneNumber = (TextView) itemView.findViewById(R.id.tvPhoneNumber);
            ibFavBtn = (ImageButton) itemView.findViewById(R.id.fav);

            ibFavBtn.setOnClickListener(new View.OnClickListener() {
                Boolean b = true;

                @Override
                public void onClick(View view) {
                    if (b == true) {
                        ibFavBtn.setImageResource(R.drawable.star2);
                        Log.e("From get", id);
                        b = false;

                    } else {
                        ibFavBtn.setImageResource(R.drawable.star);
                        b = true;
                    }
                }
            });
        }
  }

0 个答案:

没有答案