Recyclerview Onclick在单击Cardview时将特定数据传递给活动 [英] Recyclerview Onclick passing specific data when click cardview into activity
本文介绍了Recyclerview Onclick在单击Cardview时将特定数据传递给活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要做的是将数据从特定用户传递到另一个活动中,使用其用户ID作为唯一ID
它不断将相同的数据发送到其他活动中,我不知道该怎么做
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
这是 Fragment_Home 的代码,在这里我用来从Firebase
获取数据并将其填充到RecyclerView
中,以及Onclick
目前所在的位置.>
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();
这是我与recyclerview一起使用的onclick,并且可以使用.
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;
}
这是我从Firebase中获取数据来填充recyclerview的方法.
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);
}
推荐答案
尝试此代码
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;
}
这篇关于Recyclerview Onclick在单击Cardview时将特定数据传递给活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文