简体   繁体   中英

How Can I get Unique UserID generated by FireStore using Android?

package com.example.finaltry;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;
import java.util.List;

public class VendorDisplayActivity extends AppCompatActivity {

    private static final String TAG = "TAG";
    TextView textViewVendorName;
    ImageView vendorImage;
    RatingBar rating;

    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
    CollectionReference vendor_inventory_details_Ref = db.collection("Vendor_Details");
    DocumentReference ven_Ref = db.document("Vendor_Details");

    String sid ;
    RecyclerView mRecyclerView;

    //this is just for try of AP
    List<ProductModel> modelList = new ArrayList<>();
    RecyclerView.LayoutManager layoutManager;
    ProductCustomAdapter adapter;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vendor_display);

        final String vname=getIntent().getStringExtra("v_name");
        final String pid=getIntent().getStringExtra("p_id");

        Toast.makeText(getApplicationContext(),vname,Toast.LENGTH_SHORT).show();

        textViewVendorName = findViewById(R.id.textViewVendorName);
        vendorImage = findViewById(R.id.imageViewVendor);
        rating = findViewById(R.id.ratingBarVendor);
        context = this;

        mRecyclerView =findViewById(R.id.recyclerview_Product);

        //set recycler views properties
        mRecyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(layoutManager);


        vendor_inventory_details_Ref
                .whereEqualTo("Brand_Name",vname)
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                            @Override
                                           public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                               Log.d(TAG,"Step2 ");
                                               for (DocumentSnapshot doc: task.getResult()){
                                                   sid=doc.getId();
                                                   Log.d(TAG,"Step3 ");

                                               }
                                               Log.d(TAG,"Step4 ");
                                               Toast.makeText(getApplicationContext(),sid,Toast.LENGTH_SHORT).show();

                                           }
                                       }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d(TAG,"Step5 ");

                Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();

            }
        });

        //Toast.makeText(getApplicationContext(),pid,Toast.LENGTH_SHORT).show();
       // sid="N1GQdOXUSLRS1dREfu5SltVKoBb2";
        Toast.makeText(getApplicationContext(),vname,Toast.LENGTH_SHORT).show();
        vendor_inventory_details_Ref.document(sid).collection("Inventory")
                                .get()
                                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                        modelList.clear();
                                        for (DocumentSnapshot doc : task.getResult()) {
                                            ProductModel productModel = new ProductModel(doc.getString("ProductName"),
                                                    doc.getString("Rate"), doc.getString("ProductId"));
                                            modelList.add(productModel);
                                        }
                                        adapter = new ProductCustomAdapter(VendorDisplayActivity.this, modelList, context);
                                        mRecyclerView.setAdapter(adapter);
                                    }
                                }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {

                                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        });






    }
}

My question is how can we generate sid, cause when I put static value for sid, the app is running properly. Error if sid is not given is :NullPointerException if I remove comment and put static value in sid, it working Also, I am creating search function from event Manager profile to Vendor Profile, so how can this be solved.

Firebase ScreenShot:

get() is asynchronous and returns immediately before the results of the query are complete. Your callback will be invoke some time later with the results.

Right now, your code is assuming that the query happens instantly and a value for sid is immediately available after get() returns. This is not a valid assumption to make. If you want to use the result of a Firestore query, you should only use it after the query completes, which means it must only be handled within the callback, or using some other system that guarantees the followup code only triggers after the result is available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM