Kotlinboy
Kotlinboy

Reputation: 3915

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

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

Upvotes: 4

Views: 12606

Answers (2)

Gibson Ndemanga
Gibson Ndemanga

Reputation: 69

public void onBindViewHolder(final GenericViewHolder holder, int position, FarmerModel model) {      
    DocumentReference ref = getItem(position).getReference();
    final String key = ref.getId();
    ....
}

Upvotes: 0

SUPERCILEX
SUPERCILEX

Reputation: 4007

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

Upvotes: 17

Related Questions