【发布时间】:2019-08-07 17:28:22
【问题描述】:
我正在使用带有片段的 recyclerview。以下 sn-p 从 firebase 检索数据。我还在我的 recyclerview 适配器中实现了 onClickListener() 并希望启动一个意图并显示详细的配置文件视图。 (onClickListener 正在工作,因为我可以获得日志消息)。
但是 如何使用 intent.putExtra() 传递子密钥或项目 ID,以便我可以使用该子密钥或 ID 启动新活动并填充数据?
片段
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;
}
}
适配器
public class DoctorsAdapter extends RecyclerView.Adapter<DoctorsAdapter.DoctorsViewHolder> {
private Context mContext;
private List<Doctors> doctorsList;
private View.OnClickListener mOnItemClickListener;
public DoctorsAdapter(Context mContext, List<Doctors> doctorsList) {
this.mContext = mContext;
this.doctorsList = doctorsList;
}
@Override
public DoctorsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.doctors_card, parent, false);
return new DoctorsViewHolder(view);
}
@Override
public void onBindViewHolder(final DoctorsViewHolder holder, final int position) {
Doctors doctors = doctorsList.get(position);
holder.title.setText(doctors.getName());
holder.specialty.setText(doctors.getSpecialty());
Log.d("DoctorsAdapter", "spcialty "+ doctors.getSpecialty());
Glide.with(mContext).load(doctors.getThumbnail()).into(holder.thumbnail);
holder.overflow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopUpMenu(holder.overflow);
}
});
}
@Override
public int getItemCount() {
return doctorsList.size();
}
public class DoctorsViewHolder extends RecyclerView.ViewHolder{
public TextView title, specialty;
public ImageView thumbnail, overflow;
public DoctorsViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
specialty = (TextView) view.findViewById(R.id.specialty);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
overflow = (ImageView) view.findViewById(R.id.overflow);
view.setTag(this);
view.setOnClickListener(mOnItemClickListener);
}
}
public void setmOnItemClickListener(View.OnClickListener onItemClickListener) {
mOnItemClickListener = onItemClickListener;
}
}
【问题讨论】:
标签: java android json firebase-realtime-database