Passed data from Activity to Adapter, but when I try to use data as string, it displays as null

Viewed 25

I have passed data from my Activity to My Adapter. When I debug, I can see the correct data has successfully passed to my adapter, but when I attempt to use it as a string ( for example, if I want to set the text as the data I just passed), it shows as null.

  • On the line that says " this.uniquesharedIds = uniquesharedId;" - the "uniqiuesharedIds" is showing as null.
  • "uniquesharedId" shows has the successfully passed data.
  • I need to be able to use the string of "uniqiuesharedIds."

Sorry if this is a silly question. Sending data from Activities to Adapters always confuses me and Im not able to find a ton of documentation/videos on the topic. Thank you.

My Activity In the On Create Method

myadapter = new Invite_Contributors_Adapter(contributorInviteList, getIntent().getStringExtra("uniquesharedId"));

The Adapter

public class Invite_Contributors_Adapter extends RecyclerView.Adapter<Invite_Contributors_Adapter.myviewholder> {
    private ArrayList<Model_Invite_Contributors_List> model_invite_contributors_lists = new ArrayList<>();
    FirebaseAuth mAuth;
    private FirebaseUser currentuser;
    private DatabaseReference UsersReference;
    Context context;
    String uniquesharedIds;
    private InviteContributorsInterface inviteContributorsInterface;

    public Invite_Contributors_Adapter() {
    }

    public void updateInviteList (ArrayList list) {
        model_invite_contributors_lists .clear();
        model_invite_contributors_lists .addAll(list);
        notifyDataSetChanged();
    }

    public Invite_Contributors_Adapter(ArrayList<Model_Invite_Contributors_List>model_invite_contributors_lists, String uniquesharedId) {
        this.model_invite_contributors_lists = model_invite_contributors_lists;
        this.uniquesharedIds = uniquesharedId;
    }

    @NonNull
    @Override
    public Invite_Contributors_Adapter.myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout_invite_contributors_list, parent, false);
        return new myviewholder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull myviewholder holder, int position) {
        holder.setData(model_invite_contributors_lists.get(position));
        mAuth = FirebaseAuth.getInstance();
        holder.Name.setText(uniquesharedIds);
    }


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


    static class myviewholder extends RecyclerView.ViewHolder implements DialogInterface.OnClickListener {
        TextView Name;
        CircleImageView profileImageView;

        public myviewholder(@NonNull View itemView) {
            super(itemView);
            Name = itemView.findViewById(R.id.contributor_name);
            profileImageView = itemView.findViewById(R.id.member_profile_picture);
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
        }

        public void setData(Model_Invite_Contributors_List model) {
            FirebaseUser currentuser;
            currentuser = FirebaseAuth.getInstance().getCurrentUser();
            DatabaseReference NameRef = FirebaseDatabase.getInstance().getReference(Strings.UsersReference);
            NameRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()) {
                        //this is very important. this says to not show the current user in the list of people to invite as a contributor.//
                        if (currentuser.getUid().equals(model.getUser_Id())){
                            ViewGroup.LayoutParams params = itemView.getLayoutParams();
                            params.height = 0;
                            itemView.setLayoutParams(params);
                        } else {
                            for(DataSnapshot ds : dataSnapshot.getChildren())
                            {
                                itemView.setVisibility(View.VISIBLE);
                                String profileImageString;
                                profileImageString = model.getProfileimage();
                                Glide.with(profileImageView.getContext()) //pulling in image and telling the image which imageview to go to once it comes in from the database
                                        .load(profileImageString)
                                        .placeholder(R.drawable.circle_placeholder)
                                        .error(R.drawable.circle_placeholder)
                                        .into(profileImageView);
                            }
                        }
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                }

            });
        }
    }



    public void setInterface (Invite_Contributors_Adapter.InviteContributorsInterface inviteContributorsInterface) {
        this.inviteContributorsInterface = inviteContributorsInterface;
    }

    public interface InviteContributorsInterface{

    }



}
1 Answers

Are you using any primary constructor of the adapter like this?

myadapter = new Invite_Contributors_Adapter();

If yes, then that's where the problem is. If you're initializing object with two different constructors then you'll get the value of the object which you initialized later.

Make sure to check the adapter object & then proceed.

Related