Search code examples
androidandroid-intentfirebase-realtime-databaseandroid-cardview

How to transfer data from recyclerview to next activity when clicked on cardview


I have created an application in which I have retrieved multiple users from database, I am using firebase as my database. I want to transfer data from my card view to another activity when clicked on card view. When I click on card view I get all the users data from database.

These are the users

This the data inside user

The below is code of recycler view:

public class Search_Profile extends AppCompatActivity {
DatabaseReference databaseReference;
    Firebase firebase;
    FirebaseAuth mAuth;
    ProgressDialog progressDialog;
    List<User> list = new ArrayList<User>();
    RecyclerView recyclerView;
    FirebaseUser firebaseUser;
    RecyclerView.Adapter adapter ;
     Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_detail);

        b1=(Button)findViewById(R.id.viewProfile);
        mAuth = FirebaseAuth.getInstance();

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        recyclerView.setHasFixedSize(true);

        recyclerView.setLayoutManager(new LinearLayoutManager(Search_Profile.this));

        progressDialog = new ProgressDialog(Search_Profile.this);

        progressDialog.setMessage("Loading Data from Firebase Database");

        progressDialog.show();

        databaseReference = FirebaseDatabase.getInstance().getReference().child(Dashboard.Database_Path);

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                for (DataSnapshot dataSnapshot : snapshot.getChildren()) {

                    User user = dataSnapshot.getValue(User.class);
                    assert user != null;
                    System.out.println(user.getFirst_name()+" "+user.getLast_name());
                   // System.out.println();
                    System.out.println(user.getDate());
                    System.out.println(user.getUser_id());
                    System.out.println(user.getHeight());
                    System.out.println(user.getHighest_education());
                    System.out.println(user.getOccupation());
                    list.add(user);
                }
                adapter = new RecyclerViewAdapter(Search_Profile.this,list);

                recyclerView.setAdapter(adapter);


                progressDialog.dismiss();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                progressDialog.dismiss();
            }

        });

    }
}

This is the code for Recycler view adapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    DatabaseReference databaseReference;
    Firebase firebase;
    FirebaseAuth mAuth;
    Context context;
    List<User> MainImageUploadInfoList;

    RecyclerViewAdapter(Context context, List<User> TempList) {

        this.MainImageUploadInfoList = TempList;

        this.context = context;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

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

        final User user = MainImageUploadInfoList.get(position);
        holder.getLayoutPosition();
        holder.mCardView.setTag(position);
        holder.FirstNameTextView.setText(user.getFirst_name() + " " + user.getLast_name());
        holder.DateTextView.setText(user.getDate());
        holder.HeightTextView.setText(user.getHeight());
        holder.EducationTextView.setText(user.getHighest_education());
        holder.OccupationTextView.setText(user.getOccupation());
        holder.UserIDTextView.setText(user.getUser_id());
        holder.itemView.setTag(position);

    }

    @Override
    public int getItemCount() {

        return MainImageUploadInfoList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        User user = new User();

        public TextView FirstNameTextView;
        public TextView DateTextView;
        public TextView HeightTextView;
        public TextView EducationTextView;
        public TextView OccupationTextView;
        public TextView UserIDTextView;
        public Button ViewProfile;
        public View mCardView;

        public ViewHolder(@NonNull View itemView) {

            super(itemView);
            mCardView = (CardView) itemView.findViewById(R.id.cardview1);
            FirstNameTextView = (TextView) itemView.findViewById(R.id.textView1);
            DateTextView = (TextView) itemView.findViewById(R.id.textView3);
            HeightTextView = (TextView) itemView.findViewById(R.id.textView4);
            EducationTextView = (TextView) itemView.findViewById(R.id.textView5);
            OccupationTextView = (TextView) itemView.findViewById(R.id.textView6);
            UserIDTextView = (TextView) itemView.findViewById(R.id.textView7);
            ViewProfile = (Button) itemView.findViewById(R.id.viewProfile);
            mCardView.setOnClickListener(this);
        }

            @Override
            public void onClick(final View v) {

                final int position = (int) itemView.getTag();


                Toast.makeText(itemView.getContext(), Integer.toString(position), Toast.LENGTH_SHORT).show();
                databaseReference = FirebaseDatabase.getInstance().getReference().child(Dashboard.Database_Path);
                final Intent it = new Intent (v.getContext(), ViewProfile.class);
                databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                        User user = null;
                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                            user = snapshot.getValue(User.class);

                            if (getLayoutPosition() == 0) {

                                it.putExtra("first_name", user.getFirst_name());
                                it.putExtra("last_name", user.getLast_name());
                                it.putExtra("date", user.getDate());
                                it.putExtra("height", user.getHeight());
                                it.putExtra("city_state", user.getCity_state());
                                it.putExtra("hobbies", user.getHobbies());
                                it.putExtra("highest_education", user.getHighest_education());
                                it.putExtra("occupation", user.getOccupation());
                                it.putExtra("income", user.getIncome());
                                it.putExtra("marital_status", user.getMarital_status());
                                it.putExtra("family_members", user.getFamily_members());
                                it.putExtra("fathers_name", user.getFathers_name());
                                it.putExtra("mothers_name", user.getMothers_name());
                                it.putExtra("fathers_occupation", user.getFathers_occupation());
                                it.putExtra("mothers_occupation", user.getMothers_occupation());
                                it.putExtra("user_id", user.getUser_id());
                                it.putExtra("positionGroup", position);
                                context.startActivity(it);

                            } else if (getLayoutPosition() == 1) {

                                it.putExtra("first_name", user.getFirst_name());
                                it.putExtra("last_name", user.getLast_name());
                                it.putExtra("date", user.getDate());
                                it.putExtra("height", user.getHeight());
                                it.putExtra("city_state", user.getCity_state());
                                it.putExtra("hobbies", user.getHobbies());
                                it.putExtra("highest_education", user.getHighest_education());
                                it.putExtra("occupation", user.getOccupation());
                                it.putExtra("income", user.getIncome());
                                it.putExtra("marital_status", user.getMarital_status());
                                it.putExtra("family_members", user.getFamily_members());
                                it.putExtra("fathers_name", user.getFathers_name());
                                it.putExtra("mothers_name", user.getMothers_name());
                                it.putExtra("fathers_occupation", user.getFathers_occupation());
                                it.putExtra("mothers_occupation", user.getMothers_occupation());
                                it.putExtra("user_id", user.getUser_id());
                                it.putExtra("positionGroup", position);
                                context.startActivity(it);
                            } else if (getLayoutPosition() == 2) {

//                            Bundle dataBundle = new Bundle();
                                it.putExtra("first_name", user.getFirst_name());
                                it.putExtra("last_name", user.getLast_name());
                                it.putExtra("date", user.getDate());
                                it.putExtra("height", user.getHeight());
                                it.putExtra("city_state", user.getCity_state());
                                it.putExtra("hobbies", user.getHobbies());
                                it.putExtra("highest_education", user.getHighest_education());
                                it.putExtra("occupation", user.getOccupation());
                                it.putExtra("income", user.getIncome());
                                it.putExtra("marital_status", user.getMarital_status());
                                it.putExtra("family_members", user.getFamily_members());
                                it.putExtra("fathers_name", user.getFathers_name());
                                it.putExtra("mothers_name", user.getMothers_name());
                                it.putExtra("fathers_occupation", user.getFathers_occupation());
                                it.putExtra("mothers_occupation", user.getMothers_occupation());
                                it.putExtra("user_id", user.getUser_id());
                                it.putExtra("positionGroup", position);
                                context.startActivity(it);

                            } else if (getLayoutPosition() == 3) {

                            } else if (getLayoutPosition() == 4) {

                            } else if (getLayoutPosition() == 5) {

                            }
                            //or you can use For loop if you have long list of items. Use its length or size of the list as
                        }
                    }

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

                    }
                });

            }
        }
    }

This is the code of next activity:

public class ViewProfile extends AppCompatActivity {
    String user_id;
    ImageView imageView;
    TextView tv1,tv2,tv3,tv4,tv5,tv6,tv7,tv8,tv9,tv10,tv11,tv12,tv13,tv14,tv15,tv16;
    FirebaseAuth mAuth;
    //DatabaseReference uidRef;
    FirebaseDatabase database;
    User user=new User();
    DatabaseReference reference;
    public static final String Firebase_Server_URL = "https://baghbanshadi-25553.firebaseio.com/User/";
    Firebase firebase;
    public static final String Database_Path = "User";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_profile);
        imageView = findViewById(R.id.imageView4);
        tv1 = findViewById(R.id.textView14);
        tv2 = findViewById(R.id.textView15);
        tv3 = findViewById(R.id.textView16);
        tv4 = findViewById(R.id.textView17);
        tv5 = findViewById(R.id.textView18);
        tv6 = findViewById(R.id.textView19);
        tv7 = findViewById(R.id.textView20);
        tv8 = findViewById(R.id.textView21);
        tv9 = findViewById(R.id.textView22);
        tv10 = findViewById(R.id.textView23);
        tv11 = findViewById(R.id.textView24);
        tv12 = findViewById(R.id.textView25);
        tv13 = findViewById(R.id.textView26);
        tv14 = findViewById(R.id.textView27);
        tv15 = findViewById(R.id.textView28);
        tv16 = findViewById(R.id.textView29);



        String first_name = getIntent().getExtras().getString("first_name");

        String last_name = getIntent().getExtras().getString("last_name");

        String date= getIntent().getExtras().getString("date");

        String user_id= getIntent().getExtras().getString("user_id");

        String height= getIntent().getExtras().getString("height");

        String highest_education= getIntent().getExtras().getString("highest_education");

        String occupation= getIntent().getExtras().getString("occupation");

        String city_state= getIntent().getExtras().getString("city_state");

        String hobbies= getIntent().getExtras().getString("hobbies");

        String income= getIntent().getExtras().getString("income");

        String marital_status= getIntent().getExtras().getString("marital_status");

        String family_members= getIntent().getExtras().getString("family_members");

        String fathers_name= getIntent().getExtras().getString("fathers_name");

        String mothers_name= getIntent().getExtras().getString("mothers_name");

        String fathers_occupation= getIntent().getExtras().getString("fathers_occupation");

        String mothers_occupation= getIntent().getExtras().getString("mothers_occupation");


            tv1.setText(first_name);
            tv2.setText(last_name);
            tv3.setText(date);
            tv4.setText(user_id);
            tv5.setText(height);
            tv6.setText(highest_education);
            tv7.setText(occupation);
            tv8.setText(city_state);
            tv9.setText(hobbies);
            tv10.setText(income);
            tv11.setText(marital_status);
            tv12.setText(family_members);
            tv13.setText(fathers_name);
            tv14.setText(mothers_name);
            tv15.setText(fathers_occupation);
            tv16.setText(mothers_occupation);

    }
}

I want a single user on a single card view and I am getting multiple users on every single card view. What should I do?


Solution

  • I found my answer and its working, below is the code:

        public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
        DatabaseReference databaseReference;
        Firebase firebase;
        FirebaseAuth mAuth;
        Context context;
        List<User> MainImageUploadInfoList;
    
        RecyclerViewAdapter(Context context, List<User> TempList) {
            this.MainImageUploadInfoList = TempList;
            this.context = context;
        }
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler, parent, false);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }
    
        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
    
            final User user = MainImageUploadInfoList.get(position);
    
            holder.itemView.setTag(user.getUser_id());
            holder.getLayoutPosition();
            holder.mCardView.setTag(position);
            holder.FirstNameTextView.setText(user.getFirst_name() + " " + user.getLast_name());
            holder.DateTextView.setText(user.getDate());
            holder.HeightTextView.setText(user.getHeight());
            holder.EducationTextView.setText(user.getHighest_education());
            holder.OccupationTextView.setText(user.getOccupation());
            holder.UserIDTextView.setText(user.getUser_id());
            holder.income.setText(user.getIncome());
            holder.city_state.setText(user.getCity_state());
            holder.hobbies.setText(user.getHobbies());
            holder.marital_status.setText(user.getMarital_status());
            holder.family_members.setText(user.getFamily_members());
            holder.mothers_name.setText(user.getMothers_name());
            holder.fathers_name.setText(user.getFathers_name());
            holder.fathers_occupation.setText(user.getFathers_occupation());
            holder.mothers_occupation.setText(user.getMothers_occupation());
            holder.itemView.setTag(position);
    
        }
    
        @Override
        public int getItemCount() {
    
            return MainImageUploadInfoList.size();
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    
            User user = new User();
            public TextView FirstNameTextView;
            public TextView DateTextView;
            public TextView HeightTextView;
            public TextView EducationTextView;
            public TextView OccupationTextView;
            public TextView UserIDTextView;
            public Button send_invitation,shortlist_profile;
            public TextView income;
            public TextView city_state;
            public TextView hobbies;
            public TextView marital_status;
            public TextView family_members;
            public TextView mothers_name;
            public TextView fathers_name;
            public TextView fathers_occupation;
            public TextView mothers_occupation;
            public CardView mCardView;
            // RelativeLayout parentLayout;
    
            public ViewHolder(@NonNull final View itemView) {
    
                super(itemView);
                mCardView = (CardView) itemView.findViewById(R.id.cardview1);
                FirstNameTextView = (TextView) itemView.findViewById(R.id.textView1);
                DateTextView = (TextView) itemView.findViewById(R.id.textView3);
                HeightTextView = (TextView) itemView.findViewById(R.id.textView4);
                EducationTextView = (TextView) itemView.findViewById(R.id.textView5);
                OccupationTextView = (TextView) itemView.findViewById(R.id.textView6);
                UserIDTextView = (TextView) itemView.findViewById(R.id.textView7);
                income = (TextView) itemView.findViewById(R.id.income);
                city_state = (TextView) itemView.findViewById(R.id.city_state);
                hobbies = (TextView) itemView.findViewById(R.id.hobbies);
                marital_status = (TextView) itemView.findViewById(R.id.marital_status);
                family_members = (TextView) itemView.findViewById(R.id.family_members);
                mothers_name = (TextView) itemView.findViewById(R.id.mothers_name);
                fathers_name = (TextView) itemView.findViewById(R.id.fathers_name);
                fathers_occupation = (TextView) itemView.findViewById(R.id.fathers_occupation);
                mothers_occupation = (TextView) itemView.findViewById(R.id.mothers_occupation);
                send_invitation = (Button) itemView.findViewById(R.id.sendinvitation);
                shortlist_profile=(Button)itemView.findViewById(R.id.shortlistprofile);
                send_invitation=(Button)itemView.findViewById(R.id.sendinvitation);
                shortlist_profile=(Button)itemView.findViewById(R.id.shortlistprofile);
                shortlist_profile.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final Intent it=new Intent(v.getContext(),Shortlisted_Profile.class);
                        it.putExtra("first_name", FirstNameTextView.getText().toString());
                        it.putExtra("date", DateTextView.getText().toString());
                        it.putExtra("height", HeightTextView.getText().toString());
                        it.putExtra("city_state", city_state.getText().toString());
                        it.putExtra("hobbies", hobbies.getText().toString());
                        it.putExtra("highest_education", EducationTextView.getText().toString());
                        it.putExtra("occupation", OccupationTextView.getText().toString());
                        it.putExtra("income", income.getText().toString());
                        it.putExtra("marital_status", marital_status.getText().toString());
                        it.putExtra("family_members", family_members.getText().toString());
                        it.putExtra("fathers_name", fathers_name.getText().toString());
                        it.putExtra("mothers_name", mothers_name.getText().toString());
                        it.putExtra("fathers_occupation", fathers_occupation.getText().toString());
                        it.putExtra("mothers_occupation", mothers_occupation.getText().toString());
                        it.putExtra("user_id", UserIDTextView.getText().toString());
                        Toast.makeText(itemView.getContext(), "Data added to shortlisted profile", Toast.LENGTH_SHORT).show();
                        context.startActivity(it);
                    }
                });
    
                //parentLayout=itemView.findViewById(R.id.parent);
                mCardView.setOnClickListener(this);
            }
    
            @Override
            public void onClick(final View v) {
                final int position = (int) itemView.getTag();
                Toast.makeText(itemView.getContext(), Integer.toString(position), Toast.LENGTH_SHORT).show();
    
                if (getLayoutPosition() == 0) {
    
                    final Intent it = new Intent(v.getContext(), ViewProfile.class);
                    it.putExtra("first_name", FirstNameTextView.getText().toString());
                    it.putExtra("date", DateTextView.getText().toString());
                    it.putExtra("height", HeightTextView.getText().toString());
                    it.putExtra("city_state", city_state.getText().toString());
                    it.putExtra("hobbies", hobbies.getText().toString());
                    it.putExtra("highest_education", EducationTextView.getText().toString());
                    it.putExtra("occupation", OccupationTextView.getText().toString());
                    it.putExtra("income", income.getText().toString());
                    it.putExtra("marital_status", marital_status.getText().toString());
                    it.putExtra("family_members", family_members.getText().toString());
                    it.putExtra("fathers_name", fathers_name.getText().toString());
                    it.putExtra("mothers_name", mothers_name.getText().toString());
                    it.putExtra("fathers_occupation", fathers_occupation.getText().toString());
                    it.putExtra("mothers_occupation", mothers_occupation.getText().toString());
                    it.putExtra("user_id", UserIDTextView.getText().toString());
                    it.putExtra("positionGroup", position);
                    context.startActivity(it);
    
                } else if (getLayoutPosition() == 1) {
    
                    final Intent it = new Intent(v.getContext(), ViewProfile.class);
                    it.putExtra("first_name", FirstNameTextView.getText().toString());
                    // it.putExtra("last_name", user.getLast_name());
                    it.putExtra("date", DateTextView.getText().toString());
                    it.putExtra("height", HeightTextView.getText().toString());
                    it.putExtra("city_state", city_state.getText().toString());
                    it.putExtra("hobbies", hobbies.getText().toString());
                    it.putExtra("highest_education", EducationTextView.getText().toString());
                    it.putExtra("occupation", OccupationTextView.getText().toString());
                    it.putExtra("income", income.getText().toString());
                    it.putExtra("marital_status", marital_status.getText().toString());
                    it.putExtra("family_members", family_members.getText().toString());
                    it.putExtra("fathers_name", fathers_name.getText().toString());
                    it.putExtra("mothers_name", mothers_name.getText().toString());
                    it.putExtra("fathers_occupation", fathers_occupation.getText().toString());
                    it.putExtra("mothers_occupation", mothers_occupation.getText().toString());
                    it.putExtra("user_id", UserIDTextView.getText().toString());
                    it.putExtra("positionGroup", position);
                    context.startActivity(it);
    
                } else if (getLayoutPosition() == 2) {
                    final Intent it = new Intent(v.getContext(), ViewProfile.class);
                    it.putExtra("first_name", FirstNameTextView.getText().toString());
                    // it.putExtra("last_name", user.getLast_name());
                    it.putExtra("date", DateTextView.getText().toString());
                    it.putExtra("height", HeightTextView.getText().toString());
                    it.putExtra("city_state", city_state.getText().toString());
                    it.putExtra("hobbies", hobbies.getText().toString());
                    it.putExtra("highest_education", EducationTextView.getText().toString());
                    it.putExtra("occupation", OccupationTextView.getText().toString());
                    it.putExtra("income", income.getText().toString());
                    it.putExtra("marital_status", marital_status.getText().toString());
                    it.putExtra("family_members", family_members.getText().toString());
                    it.putExtra("fathers_name", fathers_name.getText().toString());
                    it.putExtra("mothers_name", mothers_name.getText().toString());
                    it.putExtra("fathers_occupation", fathers_occupation.getText().toString());
                    it.putExtra("mothers_occupation", mothers_occupation.getText().toString());
                    it.putExtra("user_id", UserIDTextView.getText().toString());
                    it.putExtra("positionGroup", position);
                    context.startActivity(it);
                }
            }
        }
    }