猿问

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

我正在使用带有片段的回收器视图。以下代码段从 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;

}


肥皂起泡泡
浏览 125回答 1
1回答

慕慕森

在您的医生POJO中再添加一个属性,这将是IDpublic 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) {&nbsp; &nbsp; this.name = name;&nbsp; &nbsp; this.specialty = specialty;&nbsp; &nbsp; this.thumbnail = thumbnail;&nbsp; &nbsp; this.id = id;}现在,当您获取数据时,获取检索到的数据的密钥,这将是您的医生IDvalueEventListener = new ValueEventListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (DataSnapshot snapshot: dataSnapshot.getChildren()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name = snapshot.child("name").getValue(String.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String specialty = snapshot.child("specialty").getValue(String.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String thumbnail = snapshot.child("thumbnail").getValue(String.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String id = snapshot.getKey();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Doctors doctor = new Doctors(id,name, specialty,thumbnail);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(doctor);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adapter.notifyDataSetChanged();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }然后在您的通过中,您的意图中ID的额外内容onClickListener()private View.OnClickListener onItemClickListener = new View.OnClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; RecyclerView.ViewHolder viewHolder = (RecyclerView.ViewHolder) view.getTag();&nbsp; &nbsp; &nbsp; &nbsp; int position = viewHolder.getAdapterPosition();&nbsp; &nbsp; &nbsp; &nbsp; Doctors doctor = list.get(position);&nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(getActivity(), DoctorProfile.class);&nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra("id",doctor.getID();&nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; }&nbsp; };
随时随地看视频慕课网APP

相关分类

Java
我要回答