[英]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,它可以工作此外,我正在创建从事件管理器配置文件到供应商配置文件的搜索功能,那么如何解决这个问题。
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.