为了账号安全,请及时绑定邮箱和手机立即绑定

如何获取火库列表对象位置?

如何获取火库列表对象位置?

肥皂起泡泡 2022-08-17 12:29:10
我正在使用带有片段的回收器视图。以下代码段从 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 parameterspublic static DoctorsFragment newInstance() {   return new DoctorsFragment();}@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setHasOptionsMenu(true);    getActivity().setTitle(R.string.nav_menu_doctors);}@Overridepublic 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;}
查看完整描述

1 回答

?
慕慕森

TA贡献1856条经验 获得超17个赞

在您的医生POJO中再添加一个属性,这将是ID


public class Doctors {


private String id;

private String name;

private String degree;

...

并更新您的构造函数(同时放置您的 setter 和 getter for id)


public Doctors(String id,String name, String specialty, String thumbnail) {

    this.name = name;

    this.specialty = specialty;

    this.thumbnail = thumbnail;

    this.id = 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();

            }

        }

然后在您的通过中,您的意图中ID的额外内容onClickListener()


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


    }

  };


查看完整回答
反对 回复 2022-08-17
  • 1 回答
  • 0 关注
  • 124 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号