亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何獲取火庫列表對象位置?

如何獲取火庫列表對象位置?

肥皂起泡泡 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 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號