ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

java – 如何在Firestore数据库中获取Android中的文档ID或名称以传递给另一个活动?

2019-10-09 05:47:56  阅读:269  来源: 互联网

标签:firebaseui android java firebase google-cloud-firestore


我正在使用FirestoreRecyclerAdapter.

我能够获得每个模型和文档属性.但我想获得文档ID.

我尝试过使用模型.自动建议,但没有文件ID或名称

我有以下DealsHolder类.这是在Kotlin.然而,所有类的其余部分都在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以便传递给另一个活动

然后是以下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.

如果您知道下一个活动中的数据结构,则可以通过标准的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()

标签:firebaseui,android,java,firebase,google-cloud-firestore
来源: https://codeday.me/bug/20191009/1876904.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有