如何获取firebase列表对象位置?

Posted

技术标签:

【中文标题】如何获取firebase列表对象位置?【英文标题】:How to get firebase list object position? 【发布时间】:2019-08-07 17:28:22 【问题描述】:

我正在使用带有片段的 recyclerview。以下 sn-p 从 firebase 检索数据。我还在我的 recyclerview 适配器中实现了 onClickListener() 并希望启动一个意图并显示详细的配置文件视图。 (onClickListener 正在工作,因为我可以获得日志消息)。

但是 如何使用 intent.putExtra() 传递子密钥或项目 ID,以便我可以使用该子密钥或 ID 启动新活动并填充数据?

Firebase structure

片段

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;


【问题讨论】:

【参考方案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;

现在,当您获取数据时,获取检索到的数据的密钥,这将是您的医生 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);

    
  ;

【讨论】:

我建议您不要将您的 Doctors 保存为 0、1、2、3 等,而是使用 FirebaseAuth 唯一 ID 或 pushKeys 是的,FirebaseAuth 唯一 ID 可能是最简单的方法,至少对我而言。我想知道自定义唯一 ID 是如何工作的。

以上是关于如何获取firebase列表对象位置?的主要内容,如果未能解决你的问题,请参考以下文章

从firebase获取列表数据,或如何构建数据

如何通过 android 将文件保存到 Firebase 托管文件夹位置以获取图像?

Swift 和 Firebase 实时数据库:如何获取对象的数量?

如何使用geofire从firebase获取位置

如何从 firebase 数据库参考中获取子节点列表?

如何使用firebase作为数据库在android中获取附近的用户名?