从Firestore数据库检索多个文档的数据

huangapple go评论74阅读模式
英文:

Retrieve Data from multiple documents from Firestore Database

问题

以下是您提供的代码的翻译:

无法从Firestore数据库中获取值试图在数据库中的条目与flat和block的值匹配时检索移动电话号码的值以下是我正在使用的代码我还附上了在LogCat文件中注册的问题请检查并帮忙

public class Test extends AppCompatActivity {

    Button btn;
    private FirebaseFirestore firestore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        firestore = FirebaseFirestore.getInstance();
        btn = (Button) findViewById(R.id.button3);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getmobile();
                //makecall();
            }
        });
    }

    private void makecall(){
        Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
        intent.setData(Uri.parse("tel:9890910640"));
        startActivity(intent);
    }

    private void getmobile() {
        firestore.collection("users").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot documentSnapshots) {
                String a = "101";
                String b = "F1A";
                for(DocumentSnapshot document: documentSnapshots){
                    if(document.getString("flat").equals(a) && document.getString("block").equals(b)){
                        String mobilefromdatabase = document.get("mobile").toString();
                        Toast.makeText(Test.this, "移动电话号码:" + mobilefromdatabase, Toast.LENGTH_LONG).show();
                        break;
                    }
                }
            }
        });
    }
}

LogCat详情:

05-05 03:59:26.721 8805-8805/? E/libprocessgroup: 无法创建并更改/acct/uid_10058:只读文件系统
05-05 03:59:26.721 8805-8805/? W/Zygote: createProcessGroup失败,内核是否缺少CONFIG_CGROUP_CPUACCT?
05-05 03:59:26.722 8805-8805/? I/art: 不在晚期启用-Xcheck:jni(已启用)
...

(此处省略了中间部分的LogCat详情)

注意:由于您要求仅返回翻译好的部分,我已删除了与翻译无关的部分,如问题描述和提示信息。如果您需要更多信息或解释,请随时提问。

英文:

Unable to fetch a value from the Firestore database. Trying to retrieve the value of the mobile number as and when the value of flat and block matches the entry in the database. Below is the code I am using. I have also attached the issues that are registered in LogCat file. Please check and help.

    public class Test extends AppCompatActivity {
Button btn;
private FirebaseFirestore firestore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
firestore = FirebaseFirestore.getInstance();
btn = (Button) findViewById(R.id.button3);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getmobile();
//makecall();
}
});
}
private void makecall(){
Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
intent.setData(Uri.parse(&quot;tel:9890910640&quot;));
startActivity(intent);
}
private void getmobile() {
firestore.collection(&quot;users&quot;).get().addOnSuccessListener(new OnSuccessListener&lt;QuerySnapshot&gt;() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
String a = &quot;101&quot;;
String b = &quot;F1A&quot;;
for(DocumentSnapshot document: documentSnapshots){
if(document.getString(&quot;flat&quot;)==a&amp;&amp;document.getString(&quot;block&quot;)==b){
String mobilefromdatabase = document.get(&quot;mobile&quot;).toString();
Toast.makeText(Test.this, &quot;mobile&quot; + mobilefromdatabase, Toast.LENGTH_LONG).show();
break;
}
}
}
});
}
}

从Firestore数据库检索多个文档的数据

Logcat Details

05-05 03:59:26.721 8805-8805/? E/libprocessgroup: failed to make and chown /acct/uid_10058: Read-only file system
05-05 03:59:26.721 8805-8805/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
05-05 03:59:26.722 8805-8805/? I/art: Not late-enabling -Xcheck:jni (already on)
05-05 03:59:26.770 8805-8805/com.example.myrwaapp I/MultiDex: VM with version 2.1.0 has multidex support
05-05 03:59:26.770 8805-8805/com.example.myrwaapp I/MultiDex: Installing application
05-05 03:59:26.770 8805-8805/com.example.myrwaapp I/MultiDex: VM has multidex support, MultiDex support library is disabled.
05-05 03:59:26.842 8805-8805/com.example.myrwaapp V/FA: Registered activity lifecycle callback
05-05 03:59:26.857 8805-8831/com.example.myrwaapp W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
05-05 03:59:26.861 8805-8805/com.example.myrwaapp I/FirebaseInitProvider: FirebaseApp initialization successful
05-05 03:59:26.907 8805-8834/com.example.myrwaapp W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
05-05 03:59:26.925 8805-8834/com.example.myrwaapp I/FirebaseAuth: [FirebaseAuth:] Preparing to create service connection to gms implementation
05-05 03:59:26.986 8805-8805/com.example.myrwaapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-05 03:59:27.010 8805-8805/com.example.myrwaapp V/FA: onActivityCreated
05-05 03:59:27.047 8805-8805/com.example.myrwaapp I/art: Rejecting re-init on previously-failed class java.lang.Class&lt;androidx.core.view.ViewCompat$2&gt;
05-05 03:59:27.047 8805-8805/com.example.myrwaapp I/art: Rejecting re-init on previously-failed class java.lang.Class&lt;androidx.core.view.ViewCompat$2&gt;
05-05 03:59:27.048 8805-8830/com.example.myrwaapp V/FA: App measurement collection enabled
05-05 03:59:27.049 8805-8830/com.example.myrwaapp V/FA: App measurement enabled for app package, google app id: com.example.myrwaapp, 1:649640616763:android:9025242f82dd4767798d1e
05-05 03:59:27.052 8805-8830/com.example.myrwaapp I/FA: App measurement initialized, version: 21028
05-05 03:59:27.052 8805-8830/com.example.myrwaapp I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
05-05 03:59:27.052 8805-8830/com.example.myrwaapp I/FA: To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app com.example.myrwaapp
05-05 03:59:27.052 8805-8830/com.example.myrwaapp D/FA: Debug-level message logging enabled
05-05 03:59:27.077 8805-8830/com.example.myrwaapp V/FA: Connecting to remote service
05-05 03:59:27.089 8805-8830/com.example.myrwaapp V/FA: Connection attempt already in progress
05-05 03:59:27.143 8805-8830/com.example.myrwaapp V/FA: Activity resumed, time: 8622127
05-05 03:59:27.145 8805-8830/com.example.myrwaapp I/FA: Tag Manager is not found and thus will not be used
05-05 03:59:27.151 8805-8838/com.example.myrwaapp D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
05-05 03:59:27.154 8805-8805/com.example.myrwaapp D/Atlas: Validating map...
05-05 03:59:27.159 8805-8830/com.example.myrwaapp V/FA: Connection attempt already in progress
05-05 03:59:27.159 8805-8830/com.example.myrwaapp V/FA: Connection attempt already in progress
05-05 03:59:27.188 8805-8838/com.example.myrwaapp I/OpenGLRenderer: Initialized EGL, version 1.4
05-05 03:59:27.188 8805-8838/com.example.myrwaapp W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
05-05 03:59:27.215 8805-8838/com.example.myrwaapp D/EGL_emulation: eglCreateContext: 0xa2003100: maj 2 min 0 rcv 2
05-05 03:59:27.240 8805-8838/com.example.myrwaapp D/EGL_emulation: eglMakeCurrent: 0xa2003100: ver 2 0 (tinfo 0xb4233e90)
05-05 03:59:27.243 8805-8838/com.example.myrwaapp D/OpenGLRenderer: Enabling debug mode 0
05-05 03:59:27.294 8805-8838/com.example.myrwaapp D/EGL_emulation: eglMakeCurrent: 0xa2003100: ver 2 0 (tinfo 0xb4233e90)
05-05 03:59:27.738 8805-8830/com.example.myrwaapp D/FA: Connected to remote service
05-05 03:59:27.738 8805-8830/com.example.myrwaapp V/FA: Processing queued up service tasks: 4
05-05 03:59:32.795 8805-8830/com.example.myrwaapp V/FA: Inactivity, disconnecting from the service
05-05 03:59:38.884 8805-8830/com.example.myrwaapp V/FA: Recording user engagement, ms: 11740
05-05 03:59:38.886 8805-8830/com.example.myrwaapp V/FA: Connecting to remote service
05-05 03:59:38.892 8805-8830/com.example.myrwaapp V/FA: Activity paused, time: 8633867
05-05 03:59:38.894 8805-8805/com.example.myrwaapp V/FA: onActivityCreated
05-05 03:59:38.913 8805-8830/com.example.myrwaapp V/FA: Connection attempt already in progress
05-05 03:59:38.913 8805-8830/com.example.myrwaapp V/FA: Activity resumed, time: 8633895
05-05 03:59:38.926 8805-8830/com.example.myrwaapp V/FA: Connection attempt already in progress
05-05 03:59:38.926 8805-8830/com.example.myrwaapp V/FA: Connection attempt already in progress
05-05 03:59:38.993 8805-8838/com.example.myrwaapp D/EGL_emulation: eglMakeCurrent: 0xa2003100: ver 2 0 (tinfo 0xb4233e90)
05-05 03:59:39.034 8805-8838/com.example.myrwaapp D/EGL_emulation: eglMakeCurrent: 0xa2003100: ver 2 0 (tinfo 0xb4233e90)
05-05 03:59:39.051 8805-8838/com.example.myrwaapp D/EGL_emulation: eglMakeCurrent: 0xa2003100: ver 2 0 (tinfo 0xb4233e90)
05-05 03:59:39.177 8805-8838/com.example.myrwaapp D/EGL_emulation: eglMakeCurrent: 0xa2003100: ver 2 0 (tinfo 0xb4233e90)
05-05 03:59:39.222 8805-8830/com.example.myrwaapp D/FA: Connected to remote service
05-05 03:59:39.222 8805-8830/com.example.myrwaapp V/FA: Processing queued up service tasks: 4
05-05 03:59:39.224 8805-8838/com.example.myrwaapp D/EGL_emulation: eglMakeCurrent: 0xa2003100: ver 2 0 (tinfo 0xb4233e90)
05-05 03:59:39.335 8805-8838/com.example.myrwaapp D/EGL_emulation: eglMakeCurrent: 0xa2003100: ver 2 0 (tinfo 0xb4233e90)
05-05 03:59:44.247 8805-8830/com.example.myrwaapp V/FA: Inactivity, disconnecting from the service
05-05 03:59:48.704 8805-8838/com.example.myrwaapp D/EGL_emulation: eglMakeCurrent: 0xa2003100: ver 2 0 (tinfo 0xb4233e90)
05-05 03:59:48.871 8805-8849/com.example.myrwaapp W/DynamiteModule: Local module descriptor class for providerinstaller not found.
05-05 03:59:48.877 8805-8849/com.example.myrwaapp I/DynamiteModule: Considering local module providerinstaller:0 and remote module providerinstaller:0
05-05 03:59:48.877 8805-8849/com.example.myrwaapp W/ProviderInstaller: Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
05-05 03:59:48.881 8805-8849/com.example.myrwaapp W/ResourcesManager: Asset path &#39;/system/framework/com.android.media.remotedisplay.jar&#39; does not exist or contains no resources.
05-05 03:59:48.881 8805-8849/com.example.myrwaapp W/ResourcesManager: Asset path &#39;/system/framework/com.android.location.provider.jar&#39; does not exist or contains no resources.
05-05 03:59:48.942 8805-8849/com.example.myrwaapp W/linker: libconscrypt_gmscore_jni.so: unused DT entry: type 0x6ffffffe arg 0x13dc
05-05 03:59:48.942 8805-8849/com.example.myrwaapp W/linker: libconscrypt_gmscore_jni.so: unused DT entry: type 0x6fffffff arg 0x2
05-05 03:59:48.948 8805-8849/com.example.myrwaapp V/NativeCrypto: Registering com/google/android/gms/org/conscrypt/NativeCrypto&#39;s 284 native methods...
05-05 03:59:48.963 8805-8820/com.example.myrwaapp I/art: Background sticky concurrent mark sweep GC freed 976(49KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 8MB/8MB, paused 5.861ms total 31.265ms
05-05 03:59:48.992 8805-8849/com.example.myrwaapp I/art: Rejecting re-init on previously-failed class java.lang.Class&lt;com.google.android.gms.org.conscrypt.Java7ExtendedSSLSession&gt;
05-05 03:59:48.992 8805-8849/com.example.myrwaapp I/art: Rejecting re-init on previously-failed class java.lang.Class&lt;com.google.android.gms.org.conscrypt.Java7ExtendedSSLSession&gt;
05-05 03:59:48.992 8805-8849/com.example.myrwaapp I/art: Rejecting re-init on previously-failed class java.lang.Class&lt;com.google.android.gms.org.conscrypt.Java8ExtendedSSLSession&gt;
05-05 03:59:48.992 8805-8849/com.example.myrwaapp I/art: Rejecting re-init on previously-failed class java.lang.Class&lt;com.google.android.gms.org.conscrypt.Java8ExtendedSSLSession&gt;
05-05 03:59:49.002 8805-8849/com.example.myrwaapp I/ProviderInstaller: Installed default security provider GmsCore_OpenSSL

答案1

得分: 0

首先,在Java中进行字符串比较时,不应使用==运算符,因为它将始终比较内存中对象的地址而不是实际值。应该使用equals()方法来进行字符串比较。

此外,如果您需要进行AND操作,不应获取users集合中的所有文档并在客户端上进行比较。相反,您应该使用如下所示的查询:

String a = "101";
String b = "F1A";
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference usersRef = rootRef.collection("users");
Query query = usersRef.whereEqualTo("flat", a).whereEqualTo("block", b);
query.get().addOnSuccessListener(/* ... */);

通过这种方式,您将仅获取flat属性值为101block属性值为F1A的文档。我认为只有一个公寓符合这个条件,因此您只会被计费一次读操作。

英文:

First of all, String comparison in Java, should not be performed using == operator, as it will always compare the address of the objects from the memory and not the actual values. String comparison should be done using equals() method.

Moreover, if you need an AND operation, you should not get all documents that exist within the users collection and make the comparison on the client. For that, you should use a query that looks exactly like this:

String a = &quot;101&quot;;
String b = &quot;F1A&quot;;
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference usersRef = rootRef.collection(&quot;users&quot;);
Query query = usersRef.whereEqualTo(&quot;flat&quot;, a).whereEqualTo(&quot;block&quot;, b);
query.get().addOnSuccessListener(/* ... */);

In this way, you are going to get only the documents where the flat property holds the value of 101 AND the value of block holds F1A. As I think that only one apartment has that constraint, you'll be billed with only one read operation.

huangapple
  • 本文由 发表于 2020年5月5日 05:01:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/61601553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定