How to get document id or name in Android in Firestore db for passing on to another activity?

自古美人都是妖i 提交于 2019-12-01 08:57:33

问题


I am using a FirestoreRecyclerAdapter.

I'm able to get each model and attributes of documents. But I want to get the document id.

I tried using model. Autosuggestions, but there was no document id or name

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)

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();

                });
            }

Here in the holder.itemView onClickListener I want to get document ID in order to pass to another activity

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);
    }
}

回答1:


Use the adapter's getSnapshots() method:

@Override
public void onBindViewHolder(MainActivity.DealsHolder holder, int position, DealsResponse model) {
    // ...
    holder.itemView.setOnClickListener(v -> {
        DocumentSnapshot snapshot = getSnapshots().getSnapshot(holder.getAdapterPosition());
        snapshot.getId();
        // ...
    }); 
}

The getId() method returns the document's id, so in collection/myDoc/someField, myDoc would be the id.

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))

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()


来源:https://stackoverflow.com/questions/47986504/how-to-get-document-id-or-name-in-android-in-firestore-db-for-passing-on-to-anot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!