我如何将数据从片段传递到适配器

现在我正在尝试将关键数据从片段传递到包含 recyclerview 的适配器,但问题是在我的情况下无法使用 bundle 或使用意图发送数据我很困惑如何传递数据..


这是我的片段


package com.example.together.fragment;





public class PetchingLoungeFragment extends Fragment {


    private static final String TAG = "PetFirendsFragment";


    RecyclerView recyclerView;

    PetchingLoungeAdapter petchingLoungeAdapter;

    List<User> userList;

    FirebaseUser firebaseUser;

    List<String> idList = new ArrayList<>();

    public String petKey;





    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

    {



        View view =  inflater.inflate(R.layout.fragment_petching_lounge, container, false);


        recyclerView = view.findViewById(R.id.recycler_view);

        recyclerView.setHasFixedSize(true);



        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Lounge").child("PetchingBunyang").child(firebaseUser.getUid()).child("PetId");



        reference.addValueEventListener(new ValueEventListener() {

            @Override

            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


                    for (DataSnapshot ds : dataSnapshot.getChildren())

                    {

                        String id = ds.getKey();


                        PetchingLoungeFragment petchingLoungeFragment = new PetchingLoungeFragment(); // Fragment 생성

                        Bundle bundle = new Bundle(1); // 파라미터는 전달할 데이터 개수

                        bundle.putString("id", id); // key , value

                        petchingLoungeFragment.setArguments(bundle);



HUWWW
浏览 90回答 2
2回答

慕尼黑的夜晚无繁华

在您的适配器类中创建一个公共函数和一个私有变量Int id;public void setId(int id) {&nbsp; &nbsp; this.id = id;}&nbsp;现在通过您的片段设置 ID&nbsp; &nbsp; petchingLoungeAdapter = new PetchingLoungeAdapter(getContext(), userList);&nbsp; &nbsp; petchingLoungeAdapter.setId(/* set ID here */) ;&nbsp; &nbsp; recyclerView.setAdapter(petchingLoungeAdapter);

慕运维8079593

你可以像这样编写适配器类的构造函数&nbsp; &nbsp; &nbsp; &nbsp;Context mContext;&nbsp; &nbsp; &nbsp; &nbsp; List<User> mUser;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String id;&nbsp; &nbsp; &nbsp; &nbsp; public PetchingLoungeAdapter(Context mContext, List<User> mUser, String id)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.mContext = mContext;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.mUser = mUser;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.id=id;&nbsp; &nbsp; &nbsp; &nbsp; }你可以从你的片段中调用这个构造函数,如下所示petchingLoungeAdapter = new PetchingLoungeAdapter(getContext(), userList,id);// pass the id here ( ds.getKey() ).&nbsp; &nbsp; &nbsp; &nbsp; recyclerView.setAdapter(petchingLoungeAdapter);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java