Recyclerview Onclick passing specific data when click cardview into activity

不打扰是莪最后的温柔 提交于 2019-12-11 15:16:10

问题


What I'm trying to do is to pass a data from a specific user into another activity using it's userID as unique ID
It keeps send the same data into the other activity, I dont know how to do it

This is code is for the Fragment_Home where I use to get data from Firebase and populate it into the RecyclerView, and where the Onclick is in at the moment.

public class FragmentRO_Home extends Fragment {

private Intent intent;
private DatabaseReference database;
RecyclerView recyclerView;
Recycler_View_Adapter adapter;
TextView TV_nodata;
private String uid;
private String nombre;
public FragmentRO_Home() {
    // Required empty public constructor    qwe
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_fragment_ro__home,container,false);
    database = FirebaseDatabase.getInstance().getReference();
    final List<Data> data = new ArrayList();

    TV_nodata = (TextView)view.findViewById(R.id.TV_result);
    recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
    adapter = new Recycler_View_Adapter(data, getActivity());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    fill_with_data();

This is the onclick which I'm using with recyclerview and it's functionable.

    recyclerView.addOnItemTouchListener(new CustomRVItemTouchListener(getActivity(), recyclerView, new RecyclerViewItemClickListener() {
        @Override
        public void onClick(View view, int position) {
            if (position == 0){
                intent = new Intent(getActivity(), ReportInformation.class);
                intent.putExtra("idData",uid);
                startActivity(intent);
            }else if (position == 1){
                intent = new Intent(getActivity(), ReportInformation.class);
                intent.putExtra("idData",uid);
                startActivity(intent);
            }else if (position == 2){
                intent = new Intent(getActivity(), ReportInformation.class);
                intent.putExtra("idData",uid);
                startActivity(intent);
            }else if (position == 3){
                intent = new Intent(getActivity(), ReportInformation.class);
                intent.putExtra("idData",uid);
                startActivity(intent);
            }else if (position == 5){
                intent = new Intent(getActivity(), ReportInformation.class);
                intent.putExtra("idData",uid);
                startActivity(intent);
            }
        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));
    return view;
}

This is the method to where I populated my recyclerview from getting data's in the firebase.

public void fill_with_data() {
    database.child("data_home").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot d) {

            if (d.exists()) {
                List<Data> list = new ArrayList<>();
                for (DataSnapshot dataw : d.getChildren()) {
                    Data data = dataw.getValue(Data.class);
                    list.add(data);

                    uid = (data.usirayd);
                    nombre = (data.title);
                }
                for (Data data : list) {
                    adapter.insert(new Data(data.title, data.description, data.condition, data.time,data.usirayd));
                }
            }else {
                TV_nodata.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    recyclerView.setAdapter(adapter);
}

回答1:


Try this code

 recyclerView.addOnItemTouchListener(new CustomRVItemTouchListener(getActivity(), recyclerView, new RecyclerViewItemClickListener() {
        @Override
        public void onClick(View view, int position) {

            if(data.size()>position){
              if (data.get(position)!= null){
                intent = new Intent(getActivity(), ReportInformation.class);
                intent.putExtra("idData",data.get(position).usirayd);
                startActivity(intent);
               }
            }
        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));
    return view;
}



回答2:


Your list is of Data type so what you need to do is make Data class parcleable and then get object form the clicked position and send it to activity. Sample code is something like this

 public void onClick(View view, int position) {
Data dataObject = list.get(position);
                intent = new Intent(getActivity(), ReportInformation.class);
                intent.putExtra("idData",uid);
intent.putExtra("Data",dataObject);
                startActivity(intent);

        }

If you want to send single id of clicked position then just get Id from list object and send like this

 uid= list.get(postion).getUserId;

and send it in intent

and in other class/activity receice that parcelable object like this

Data dataObjectReceived = getIntent().getExtras().getParcelable("Data");

and if you want to get single entity then get it by its key




回答3:


When clicking on the cardView you want to pass the data into your activity or fragment for that you need to make one interface for example in my case may use one FriendModel

Interface

public interface OnFriendListClickListener {
    void onItemClick(FriendModel model, int position);
}

Activity

public class FriendListActivity extends AppCompactActivity
        implements
        OnFriendListClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_friend);

        //...
        rvRemoveFriendList.setAdapter(new FriendListAdapter(context, list, this));

    }

    @Override
    public void onItemClick(FriendModel model, int position) {
    //your code here
    }
}

Adapter

public class FriendListAdapter extends RecyclerView.Adapter<FriendListAdapter.FriendListHolder> {

    private List<FriendModel> friendList;
    private Context context;
    private OnFriendListClickListener listener;

    public FriendListAdapter(Context context,
                             List<FriendModel> friendList,
                             OnFriendListClickListener listener,) {
        this.context = context;
        this.friendList = friendList;
        this.listener = listener;
    }

    @Override
    public FriendListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new FriendListHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row_friend, parent, false));
    }

    @Override
    public void onBindViewHolder(final FriendListHolder holder, int position) {
        holder.tvFriendNumber.setText(friendList.get(position).getFriendMobile());

        holder.llFriendListItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onItemClick(friendList.get(holder.getAdapterPosition()),
                        holder.getAdapterPosition());
            }
        });

    }

  //......     

}


来源:https://stackoverflow.com/questions/45853185/recyclerview-onclick-passing-specific-data-when-click-cardview-into-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!