0

I am stuck need your kind help, i have an activity in android user sign up, and apart from general information i have a list of existing users/entities shown in recyclerview and each item of recyclerview list contains a follow button, i want to captures the position/ buttons that were clicked to check which users/entities the new user has followed. Any suggest how to do it?

I had customer adapter and recycler view, Check the attached image for clarity. I want to create an array list that will contain ids of each list item whose button is clicked.

RecyclerView containing list item and follow button

//this adapter use on admin public class FollowLocalBusinessAdapter extends RecyclerView.Adapter {

AlertDialog.Builder alertDialogBuilder;
private Context context;
private List<FollowLocalBusinessMC> list;
private LayoutInflater layoutInflater;
private DatabaseReference mDatabase;//database reference
private FollowLocalBusinessMC localBusinessMC;
private ListAdapterListener mListener;

public FollowLocalBusinessAdapter(Context context, List<FollowLocalBusinessMC> list) {
    this.context = context;
    this.list = list;
    layoutInflater = LayoutInflater.from(context);
    alertDialogBuilder = new AlertDialog.Builder(context);

}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) {
    View view = layoutInflater.inflate(R.layout.list_item_local_business, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(final FollowLocalBusinessAdapter.MyViewHolder holder, final int position)//is called
{
    localBusinessMC = list.get(position);
    if (holder != null) {
        if (holder.followBtn.getText().equals("Follow")) {
            holder.followBtn.setBackgroundResource(R.drawable.follow_btn_bg);
            holder.followBtn.setTextColor(context.getResources().getColor(R.color.colorBlueButton));
        } else {
            holder.followBtn.setBackgroundResource(R.drawable.following_btn_bg);
            holder.followBtn.setTextColor(context.getResources().getColor(R.color.colorWhite));
        }
        holder.mainHeading.setText(localBusinessMC.getMainHeading());
        holder.subHeading.setText(localBusinessMC.getSubHeading());
        Glide.with(context).load(localBusinessMC.getIcon()).into(holder.icon);
        holder.followBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "" + list.get(position).getId(), Toast.LENGTH_SHORT).show();
                if (holder.followBtn.getText().equals("Follow")) {
                    holder.followBtn.setText("Following");
                    holder.followBtn.setTextColor(context.getResources().getColor(R.color.colorWhite));
                    holder.followBtn.setBackgroundResource(R.drawable.following_btn_bg);

                } else {
                    holder.followBtn.setText("Follow");
                    holder.followBtn.setBackgroundResource(R.drawable.follow_btn_bg);
                    holder.followBtn.setTextColor(context.getResources().getColor(R.color.colorBlueButton));
                }

            }
        });
    }
}

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


public interface ListAdapterListener { // create an interface
    void onClickAtOKButton(int position); // create callback function
}

class MyViewHolder extends RecyclerView.ViewHolder {

    TextView mainHeading, subHeading;
    ImageView icon;
    Button followBtn;

    MyViewHolder(View v) {
        super(v);
        mainHeading = v.findViewById(R.id.main_lbsr_id);
        subHeading = v.findViewById(R.id.sub_lbsr_id);
        icon = v.findViewById(R.id.icon_lbsr_id);
        followBtn = v.findViewById(R.id.followBtn_lbsr_id);
    }
}

}

1 Answers1

0

create an interface in your Adapter with one method :

public interface CallBack{
        void onItemClicked(int position , /*anyThing else*/);
    }

then get an instance of interface in your Adapter :

 private YourAdapter.CallBack;

in your Adpater constructor get object of CallBack :

    public YourAdapter(Context context, List</* */> list, YourAdapter.CallBackcallback callBack) {
            this.context = context;
            this.list= list;
            this.callback = callback;
        }

 @Override
    public void onBindViewHolder(@NonNull  ChoosenExerciseViewHolder holder, final int position) {

        holder.View.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                callback.onItemClicked(
              //pass any parameter youe defined 
              //for ex :  position , ///);
            }
        });

  }

then in your Activity/Fragment :

private YourAdapter.CallBack callback= (position, /*  */  ) -> {
            // do what you want
        };


        YourAdapter adapter = new YourAdapter(getContext(), calback, list);

       receyclerview.setAdapter(adapter )
mohosyny
  • 962
  • 1
  • 9
  • 19