android-fragments 将RecylerView项目数据发送到另一个片段,我正在从Firebase实时数据库获取数据

guicsvcw  于 2022-11-13  发布在  Android
关注(0)|答案(1)|浏览(112)

***适配器***这是我的适配器类。我正在从Firebase实时数据库获取数据。当我单击recyclerView项目时,我希望该项目的数据显示在另一个片段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***这是我设置我的循环视图的片段。

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;
    }

}

***片段***这是片段,我需要RecyclerView数据。并在我的文本视图中显示它。当用户单击RecyclerView项目时。

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;
    }
}
tquggr8v

tquggr8v1#

非常简单。在适配器类中编写以下代码

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);
            }
                });

将此代码写入要显示数据的片段中

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

相关问题