Home > database >  Send RecylerView Items Data To Another Fragment. I Am Getting Data From Firebase RealTime Database
Send RecylerView Items Data To Another Fragment. I Am Getting Data From Firebase RealTime Database

Time:09-13

Adapter This is my Adapter Class .I am Getting data from Firebase Realtime Database. When i click on the recyclerView Items I want that items Data To Show on Another Fragments TextView.

public class MyAdapter_2 extends RecyclerView.Adapter<MyAdapter_2.MyViewholder> {

    Context context;
    ArrayList<Model> list;

    public MyAdapter_2(Context context, ArrayList<Model> list) {
        this.context = context;
        this.list = list;
    }


    @NonNull
    @Override
    public MyAdapter_2.MyViewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


        View v = LayoutInflater.from(context).inflate(R.layout.fetch_data, parent, false);

        return new MyViewholder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter_2.MyViewholder holder, int position) {
//        Chat_Fragment chat_fragment = new Chat_Fragment();


        Model model = list.get(position);

        holder.speach.setText(model.getSpeach());


        holder.remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                FirebaseAuth mAuth;
                String userId;

                mAuth = FirebaseAuth.getInstance();
                userId = mAuth.getCurrentUser().getUid();

                AlertDialog.Builder builder = new AlertDialog.Builder(holder.speach.getContext());
                builder.setTitle("Are you sure !");
                builder.setMessage("Deleted data cant be undo");
                builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        FirebaseDatabase.getInstance().getReference()
                                .child("User "   userId)
                                .child(model.getKey())
                                .removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {
                                        if (task.isSuccessful()) {
                                            Toast.makeText(v.getContext(), "Deleted", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                }).addOnFailureListener(new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                        Toast.makeText(v.getContext(), "Failed  "   e, Toast.LENGTH_SHORT).show();
                                    }
                                });
                    }
                }).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
            }
        });


    }

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


    public class MyViewholder extends RecyclerView.ViewHolder {


        TextView speach, fortest;
        TextView remove;

        public MyViewholder(@NonNull View itemView) {
            super(itemView);

            speach = itemView.findViewById(R.id.savedDialogue);
            remove = itemView.findViewById(R.id.remove);


        }


    }


}

BottomSheetFragment This tis the Fragment Where i set My RecyclerView.

public class Bottom_recycler extends BottomSheetFragment {

    RecyclerView recyclerView2;
    FirebaseAuth mAuth;


    DatabaseReference root;

    MyAdapter_2 MyAdapter_2;
    ArrayList<Model> list = new ArrayList<>();

    String userId;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_bottom_recycler, container, false);


        mAuth = FirebaseAuth.getInstance();
        userId = mAuth.getCurrentUser().getUid();


        recyclerView2 = view.findViewById(R.id.recyclerView2);
        recyclerView2.setLayoutManager(new LinearLayoutManager(getContext()));

        MyAdapter_2 = new MyAdapter_2(getContext(), list);
        recyclerView2.setHasFixedSize(true);
        recyclerView2.setAdapter(MyAdapter_2);


        root = FirebaseDatabase.getInstance().getReference().child("User "   userId);
        root.keepSynced(true);

        root.addValueEventListener(new ValueEventListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                list.clear();
                for (DataSnapshot snapshot1 : snapshot.getChildren()) {

                    Model model = snapshot1.getValue(Model.class);

                    list.add(model);

                }
                MyAdapter_2.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(getContext(), ""   error, Toast.LENGTH_SHORT).show();
            }
        });


        return view;
    }


}

Fragment This is The fragment, I want That RecyclerView data . And Show it in my Text View. When User click RecyclerView Item.

public class bok_Fragment extends Fragment {

    TextView bokData;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
      View view = inflater.inflate(R.layout.fragment_bok_, container, false);



        bokData = view.findViewById(R.id.bokData);



       return view;
    }
}

CodePudding user response:

It's pretty much simple. Write the following code in your adapter class

 holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
         Intent intent = new Intent(context, bok_Fragment.class)                 
         intent.putExtra("fortest",list.get(position).getFortest());
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(intent);
            }
                });

Write this code in the Fragment in which you want to show the data

String anyname= getIntent().getStringExtra("fortest");
bokData.setText(anyname)
  • Related