私はFirestoreRecyclerAdapter
を使用しています。Firestore dbでAndroidの文書IDや名前を取得して別のアクティビティに渡す方法は?
ドキュメントの各モデルと属性を取得できます。しかし、私は文書IDを取得したい。
私はmodel.
Autosuggestionsを使用してみましたが、何の文書IDまたは名前
は、私は、次のDealsHolderクラスを持っていませんでした。これはKotlinにある。しかし、すべてのクラスの残りの部分はjavaになっています。私はのonCreate()で初期化
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)
次の関数
私は別のアクティビティに合格するには、ドキュメントのIDを取得したい。ここholder.itemView onClickListenerで
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();
});
}
その後、以下のDealsHolder。 collection/myDoc/someField
で、myDoc
がIDになるので、
@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を返します。
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);
}
}
'snapshot.getIdを何種類(); 'return? IDEで自動的にIDEにどのような型が返されるか知る方法はありますか? – Kotlinboy
'String id =(String)snapshot.getId();' 'id'は別のアクティビティをインテントとして渡すことはできますか? – Kotlinboy
これはRecycleの子のIDではなく、データベース内のFireStoreドキュメントのIDです。 Firestore DBコールを使用してアイテムを取得できるように、データベースアイテムIDが必要です。 dbのPKと同様です。 – Kotlinboy