我正在使用带有片段的recyclerview。以下代码段从Firebase检索数据。我还在recyclerview适配器中实现了onClickListener(),并希望启动一个意图并显示详细的配置文件视图。 (onClickListener正在运行,因为我可以获取日志消息)。
但是 如何使用intent.putExtra()传递子键或项ID,以便可以启动新活动并使用该子键或ID填充数据?
片段
dd$id <- cumsum(dd$day_after_rainfall==1)
print(dd)
# date day_after_rainfall id
# 1 2018-03-04 1 1
# 2 2018-03-05 2 1
# 3 2018-03-06 3 1
# 4 2018-03-07 4 1
# 5 2018-03-09 1 2
# 6 2018-03-11 1 3
# 7 2018-03-15 1 4
# 8 2018-03-16 2 4
# 9 2018-03-17 3 4
医生班
public class DoctorsFragment extends Fragment {
private List<Doctors> list;
private DoctorsAdapter adapter;
private DatabaseReference databaseReference;
private ValueEventListener valueEventListener;
private SearchView searchView = null;
private SearchView.OnQueryTextListener queryTextListener;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
* @return A new instance of fragment DoctorsFragment.
*/
// TODO: Rename and change types and number of parameters
public static DoctorsFragment newInstance() {
return new DoctorsFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
getActivity().setTitle(R.string.nav_menu_doctors);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.blank_fragment, container, false);
databaseReference = FirebaseDatabase.getInstance().getReference();
list = new ArrayList<>();
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
adapter = new DoctorsAdapter(getContext(), list);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPixel(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
adapter.setmOnItemClickListener(onItemClickListener);
asynctask();
return view;
}
private void asynctask() {
databaseReference = databaseReference.child("doctors");
valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
String name = snapshot.child("name").getValue(String.class);
String specialty = snapshot.child("specialty").getValue(String.class);
String thumbnail = snapshot.child("thumbnail").getValue(String.class);
Doctors doctor = new Doctors(name, specialty,thumbnail);
list.add(doctor);
adapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
databaseReference.addListenerForSingleValueEvent(valueEventListener);
}
private View.OnClickListener onItemClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
RecyclerView.ViewHolder viewHolder = (RecyclerView.ViewHolder) view.getTag();
int position = viewHolder.getAdapterPosition();
Doctors doctor = list.get(position);
Intent intent = new Intent(getActivity(), DoctorProfile.class);
}
};
}
适配器
public class Doctors {
private String name;
private String degree;
private String departments;
private String bio;
private String email;
private String specialty;
private String thumbnail;
public Doctors(){} // no-argument constructor
public Doctors(String name, String departments, String bio, String degree, String specialty, String thumbnail, String email) {
this.name = name;
this.departments = departments;
this.bio = bio;
this.degree = degree;
this.email = email;
this.specialty = specialty;
this.thumbnail = thumbnail;
}
public Doctors(String name, String specialty, String thumbnail) {
this.name = name;
this.specialty = specialty;
this.thumbnail = thumbnail;
}
public String getName() {
return name;
}
public String getSpecialty() {
return specialty;
}
public String getThumbnail() {
return thumbnail;
}
public String getDegree() {
return degree;
}
public String getDepartments() {
return departments;
}
public String getBio() {
return bio;
}
public String getEmail() {
return email;
}
}
答案 0 :(得分:1)
在您的Doctor POJO中再添加一个属性,这就是ID
public class Doctors {
private String id;
private String name;
private String degree;
...
并更新您的构造函数(还将id的setter和getter放置进去)
public Doctors(String id,String name, String specialty, String thumbnail) {
this.name = name;
this.specialty = specialty;
this.thumbnail = thumbnail;
this.id = id;
}
现在,当您获取数据时,获取检索到的数据的密钥,这就是您的医生身份
valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
String name = snapshot.child("name").getValue(String.class);
String specialty = snapshot.child("specialty").getValue(String.class);
String thumbnail = snapshot.child("thumbnail").getValue(String.class);
String id = snapshot.getKey();
Doctors doctor = new Doctors(id,name, specialty,thumbnail);
list.add(doctor);
adapter.notifyDataSetChanged();
}
}
然后在您的onClickListener()
中传递您意图中多余的ID
private View.OnClickListener onItemClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
RecyclerView.ViewHolder viewHolder = (RecyclerView.ViewHolder) view.getTag();
int position = viewHolder.getAdapterPosition();
Doctors doctor = list.get(position);
Intent intent = new Intent(getActivity(), DoctorProfile.class);
intent.putExtra("id",doctor.getID();
startActivity(intent);
}
};