简体   繁体   English

如何使用 Android 获取 FireStore 生成的唯一用户 ID?

[英]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.我的问题是我们如何生成 sid,因为当我为 sid 设置静态值时,应用程序运行正常。 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.如果 sid 未给出,则错误是 :NullPointerException 如果我删除注释并将静态值放入 sid,它可以工作此外,我正在创建从事件管理器配置文件到供应商配置文件的搜索功能,那么如何解决这个问题。

Firebase ScreenShot: Firebase 截图:

get() is asynchronous and returns immediately before the results of the query are complete. get()是异步的,并在查询结果完成之前立即返回。 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.现在,您的代码假设查询立即发生并且sid的值在get()返回后立即可用。 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.如果你想使用 Firestore 查询的结果,你应该只在查询完成后使用它,这意味着它只能在回调中处理,或者使用其他一些保证后续代码只在结果可用后触发的系统.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从Firebase中的.push()获取随机生成的用户ID - How do I get randomly generated userID from .push() in Firebase 如何在 Firestore 中自动增加用户 ID? - How to autoincrement userID in Firestore? 如何使用Firestore在片段中使用RecyclerView-Android - How can I use RecyclerView in fragments using Firestore - Android 如何使用getTranslatedSQLString获取Toplink生成的查询? - How can I get Toplink generated query by using getTranslatedSQLString? 如何从 Android java 的 FIrestore 中的不同文档中获取相同的字段 - How can I get same field from different document in FIrestore in Android java 如何使用用户ID访问镜像服务? - How can I access a mirror service with userid? 如果我使用Query在“Android”中拥有用户ID arraylist,如何从“Firestore”集合中获取特定的文档列表 - How to get specific list of documents from “Firestore” collection if i have users ID arraylist in “Android” using Query 如何使用 Android 从 Cloud Firestore 中的地图中检索特定字段? - How can I retrieve a specific field from a map in Cloud Firestore using Android? 如何从 Android 中的 Firestore 获取数组(数字) - How do I get an Array(Number) from Firestore in Android 我如何在我的 Android 应用程序中加入 Firestore - How I can do join with Firestore in my Android application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM