Ad

How Can I Pass The Data From Fragment To Adapter

- 1 answer

Now i'm trying to pass the key data from fragment to adapter that included recyclerview but the problem is in my case can't using bundle or send the data using by intent i'm confused how to pass the data it..

This is my fragment

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




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

                        reference1.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                idList.clear();
                                for (DataSnapshot snapshot : dataSnapshot.getChildren())
                                {
                                    idList.add(snapshot.getKey());

                                }

                                showUsers();

                            }

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

                            }
                        });

                    }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.d(TAG, "펫키에러");
            }
        });


        Log.d(TAG, "하마: "+petKey);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        userList = new ArrayList<>();
        petchingLoungeAdapter = new PetchingLoungeAdapter(getContext(), userList);
        recyclerView.setAdapter(petchingLoungeAdapter);



        return view;

    }




    private void showUsers(){
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                userList.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                    User user = snapshot.getValue(User.class);
                    for (String id : idList){
                        if (user.getId().equals(id)){
                            userList.add(user);
                        }
                    }
                }
                Collections.reverse(userList);
                petchingLoungeAdapter.notifyDataSetChanged();
            }

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

            }
        });
    }




}





From above i want to send 'id' data

String id = ds.getKey(); 

This is my Adapter

package com.example.together.adapter;

public class PetchingLoungeAdapter extends RecyclerView.Adapter<PetchingLoungeAdapter.MyViewHolder> {


    private static final String TAG = "PetchingBunyangAdapter";

    FirebaseUser firebaseUser;
    DatabaseReference reference;

    Context mContext;
    List<User> mUser;

    public PetchingLoungeAdapter(Context mContext, List<User> mUser)
    {
        this.mContext = mContext;
        this.mUser = mUser;
    }



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



        View view = LayoutInflater.from(mContext).inflate(R.layout.list_petching_lounge, parent, false);
        return new PetchingLoungeAdapter.MyViewHolder(view);

    }

    @Override
    public void onBindViewHolder(MyViewHolder viewHolder, int position)
    {

        final User user = mUser.get(position);
        user.getId();

        Log.d(TAG, "빼오냥"+user.getId());

        getRequestorUserInfo(mUser.get(position).getId(), viewHolder.img_requestor , viewHolder.requestor_name);



        viewHolder.img_requestor.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                Intent intent = new Intent(v.getContext(), PetchingLoungeDetailInfoActivity.class);

                intent.putExtra("requester_name",mUser.get(position).getFullname());
                intent.putExtra("requester_img", mUser.get(position).getImageurl());
                intent.putExtra("requester_intro", mUser.get(position).getBio());
                // intent.putExtra("requester_pet_id",mUser.get(position).getUid)

                mContext.startActivity(intent);

            }
        });

    }

    @Override
    public int getItemCount()
    {
        return mUser.size();
                //mPetchingLounge.size();
    }



    public class MyViewHolder extends RecyclerView.ViewHolder
    {

        public Button allow, refuse;
        public ImageView img_requestor;
        public TextView requestor_name;

        public MyViewHolder(View itemView)
        {
            super(itemView);

            img_requestor = itemView.findViewById(R.id.img_requestor);

            allow = itemView.findViewById(R.id.allow);
            refuse = itemView.findViewById(R.id.refuse);

            requestor_name = itemView.findViewById(R.id.requestor_name);

        }
    }



    // 펫 정보 이름, 나이, 견종, 성별,
    private void getRequestorUserInfo(String userid, final ImageView img_requestor, final TextView requestor_name)
    {

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

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                User user = dataSnapshot.getValue(User.class);

                Glide.with(mContext).load(user.getImageurl()).into(img_requestor);

                requestor_name.setText(user.getUsername());
            }

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

            }
        });

    }



}

I already try to using Bundle but it failed to using it ... How can i send that data ?

Ad

Answer

Create a public function and a private variable in your adapter class

Int id;

public void setId(int id) {
    this.id = id;
} 

Now set the ID through your fragment

    petchingLoungeAdapter = new PetchingLoungeAdapter(getContext(), userList);
    petchingLoungeAdapter.setId(/* set ID here */) ;
    recyclerView.setAdapter(petchingLoungeAdapter);
Ad
source: stackoverflow.com
Ad