如何在 Firestore 数据库的 Android 中获取文档 ID 或名称以传递给另一个 activity?
How to get document id or name in Android in Firestore db for passing on to another activity?
我正在使用 FirestoreRecyclerAdapter
。
我能够获取文档的每个模型和属性。但我想获取文档 ID。
我尝试使用 model.
自动建议,但没有文档 ID 或名称
我有以下 DealsHolder class。这是在科特林。然而,所有 classes 的其余部分都在 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() 中初始化的以下函数
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 以便传递给另一个 activity
然后是下面的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()方法returns文档的id,所以在collection/myDoc/someField
中,myDoc
就是id。
如果您知道下一个 activity 中的数据结构,则可以通过标准 firestore.collection("foo").document("bar")
方法重新创建具有该 ID 的引用。如果您正在寻找通用解决方案,我会使用 getPath()
一堆:
fun Bundle.putRef(ref: DocumentReference) = putString(REF_KEY, ref.path)
fun Bundle.getRef() = FirebaseFirestore.getInstance().document(getString(REF_KEY))
如果您想要模型中的 id,请使用自定义 SnapshotParser
:
val options = FirestoreRecyclerOptions.Builder<DealsResponse>()
.setQuery(query) {
it.toObject(DealsResponse::class.java).apply { id = it.id }
}
.build()
我正在使用 FirestoreRecyclerAdapter
。
我能够获取文档的每个模型和属性。但我想获取文档 ID。
我尝试使用 model.
自动建议,但没有文档 ID 或名称
我有以下 DealsHolder class。这是在科特林。然而,所有 classes 的其余部分都在 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() 中初始化的以下函数
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 以便传递给另一个 activity
然后是下面的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()方法returns文档的id,所以在collection/myDoc/someField
中,myDoc
就是id。
如果您知道下一个 activity 中的数据结构,则可以通过标准 firestore.collection("foo").document("bar")
方法重新创建具有该 ID 的引用。如果您正在寻找通用解决方案,我会使用 getPath()
一堆:
fun Bundle.putRef(ref: DocumentReference) = putString(REF_KEY, ref.path)
fun Bundle.getRef() = FirebaseFirestore.getInstance().document(getString(REF_KEY))
如果您想要模型中的 id,请使用自定义 SnapshotParser
:
val options = FirestoreRecyclerOptions.Builder<DealsResponse>()
.setQuery(query) {
it.toObject(DealsResponse::class.java).apply { id = it.id }
}
.build()