问题描述
我正在使用FirestoreRecyclerAdapter
.
我能够获取文档的每个模型和属性.但我想获取文档ID.
I'm able to get each model and attributes of documents. But I want to get the document id.
我尝试使用model.
自动提示,但是没有文档ID或名称
I tried using model.
Autosuggestions, but there was no document id or name
我有以下DealsHolder类.这是在科特林.但是其余所有类都在java中.
I have the following DealsHolder class. This is in Kotlin. However the rest of all the classes are in java.
package com.guidoapps.firestorerecycleradaptersample
import com.google.firebase.firestore.IgnoreExtraProperties
@IgnoreExtraProperties
data class DealsResponse(var title:String? = null,
var price:String? = null,
var dealPrice:String? = null,
var image:String? = null,
var description: String? = null)
我在onCreate()中初始化的以下函数
The following function which I initialize in onCreate()
private void getDealsList(){
Query query = db.collection("deals").orderBy("dateTime", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<DealsResponse> response = new FirestoreRecyclerOptions.Builder<DealsResponse>()
.setQuery(query, DealsResponse.class)
.build();
adapter = new FirestoreRecyclerAdapter<DealsResponse, MainActivity.DealsHolder>(response) {
@Override
public void onBindViewHolder(MainActivity.DealsHolder holder, int position, DealsResponse model) {
progressBar.setVisibility(View.GONE);
holder.textTitle.setText(model.getTitle());
holder.textPrice.setText(model.getPrice());
holder.textDesc.setText(model.getDescription());
holder.textDealPrice.setText(model.getDealPrice());
holder.textPrice.setPaintFlags(holder.textPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
Glide.with(getApplicationContext())
.load(model.getImage())
.into(holder.imageView);
holder.itemView.setOnClickListener(v -> {
Snackbar.make(DealsList, model.getTitle(), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
});
}
在holder.itemView onClickListener中,我想获取文档ID以便传递给另一个活动
然后选择以下DealsHolder.
Then the following DealsHolder.
public class DealsHolder extends RecyclerView.ViewHolder {
@BindView(R.id.title)
TextView textTitle;
@BindView(R.id.thumbnail)
ImageView imageView;
@BindView(R.id.description)
TextView textDesc;
@BindView(R.id.price)
TextView textPrice;
@BindView(R.id.dealPrice)
TextView textDealPrice;
public DealsHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
推荐答案
使用适配器的getSnapshots()
方法:
@Override
public void onBindViewHolder(MainActivity.DealsHolder holder, int position, DealsResponse model) {
// ...
holder.itemView.setOnClickListener(v -> {
DocumentSnapshot snapshot = getSnapshots().getSnapshot(holder.getAdapterPosition());
snapshot.getId();
// ...
});
}
getId() 方法返回文档的ID,因此在collection/myDoc/someField
中,myDoc
将是该ID.
The getId() method returns the document's id, so in collection/myDoc/someField
, myDoc
would be the id.
如果您知道下一个活动中的数据结构,则可以通过标准的firestore.collection("foo").document("bar")
方法重新创建具有该ID的引用.如果您正在寻找一般的解决方案,请使用getPath()
一堆:
If you know your data structure in the next activity, you can recreate the reference with that id through the standard firestore.collection("foo").document("bar")
methods. If you're looking for the general solution, I use getPath()
a bunch:
fun Bundle.putRef(ref: DocumentReference) = putString(REF_KEY, ref.path)
fun Bundle.getRef() = FirebaseFirestore.getInstance().document(getString(REF_KEY))
如果要在模型中使用ID,请使用自定义SnapshotParser
:
If you want the id in your model, use a custom SnapshotParser
:
val options = FirestoreRecyclerOptions.Builder<DealsResponse>()
.setQuery(query) {
it.toObject(DealsResponse::class.java).apply { id = it.id }
}
.build()
这篇关于如何在Android的Firestore数据库中获取文档ID或名称以传递到其他活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!