在Android和Firebase中的查询

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

query in android and Firebase

问题

以下是您要求的翻译部分:

我对这部分有很多问题。
首先,这是我的代码:

private void getUserDetails(UserObject mContact) {
    DatabaseReference mUserDB = FirebaseDatabase.getInstance().getReference().child("user");
    Query query = mUserDB.orderByChild("phone").equalTo(mContact.getPhone());
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                String phone = "",
                        name = "";
                for(DataSnapshot childSnapshot : dataSnapshot.getChildren()){
                    if(childSnapshot.child("phone").getValue()!=null)
                        phone = childSnapshot.child("phone").getValue().toString();
                    if(childSnapshot.child("name").getValue()!=null)
                        name = childSnapshot.child("name").getValue().toString();

                    UserObject mUser = new UserObject(childSnapshot.getKey(), name, phone);
                    if (name.equals(phone))
                        for(UserObject mContactIterator : contactList){
                            if(mContactIterator.getPhone().equals(mUser.getPhone())){
                                mUser.setName(mContactIterator.getName());
                            }
                        }

                    userList.add(mUser);
                    mUserListAdapter.notifyDataSetChanged();
                    return;
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

我第一个问题:我们什么时候以及为什么要使用查询(query)?

我第二个问题:orderByChild 方法和 equalTo 方法分别是什么作用?

我第三个问题:查询(query)类似于数据快照(data snapshot)还是其他类似的内容,它本身保存着什么类型的数据?

我第四个问题:query.addListenerForSingleValueEvent 做了什么?在什么时候运行其中的代码?我知道它在有更改时运行(对于 onDataChange 方法),但是我不知道这里的“更改”是指什么,例如它是指 Firebase 中我的用户列表的更改,还是指 Firebase 中实时数据库的更改?

我第五个问题:Query query = mUserDB.orderByChild("phone").equalTo(mContact.getPhone()); 是做什么的?如果不使用 equalTo 方法会发生什么?

我第六个问题:如果我们使用 Log.d("print", query.toString()) 会发生什么?

对于这么多问题我感到抱歉,但我是 Android 和 Java 的初学者,不太清楚如何解决这个问题。如果您能够回答我的问题,我会非常感谢。

感谢您阅读我的问题,如果有任何拼写问题,我在这里表示抱歉。

再次感谢您。

英文:

I have many question about this part.
at first this is my code:

private void getUserDetails(UserObject mContact) {
DatabaseReference mUserDB = FirebaseDatabase.getInstance().getReference().child("user");
Query query = mUserDB.orderByChild("phone").equalTo(mContact.getPhone());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
String  phone = "",
name = "";
for(DataSnapshot childSnapshot : dataSnapshot.getChildren()){
if(childSnapshot.child("phone").getValue()!=null)
phone = childSnapshot.child("phone").getValue().toString();
if(childSnapshot.child("name").getValue()!=null)
name = childSnapshot.child("name").getValue().toString();
UserObject mUser = new UserObject(childSnapshot.getKey(), name, phone);
if (name.equals(phone))
for(UserObject mContactIterator : contactList){
if(mContactIterator.getPhone().equals(mUser.getPhone())){
mUser.setName(mContactIterator.getName());
}
}
userList.add(mUser);
mUserListAdapter.notifyDataSetChanged();
return;
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}

my first question: when and why do we use query?

my second question: what does orderByChild method and equalTo method do?

my third question: Is query like data snapShot or something like that and what kind of data it holds in itself?

my 4 question: what does query.addListenerForSingleValueEvent do and when the code inside that run? I know it runs when there is change (for onDataChange method) but I don't what change is it for example is it change in my user list in Firebase or in my realtime database in Firebase.

my 5 question : what does Query query = mUserDB.orderByChild("phone").equalTo(mContact.getPhone()); do? and what will happen if we doesn't use method equelTo?

my 6 question: what would happen if we use Lod.d("print" , query.toString())

excuse me for having a lot of questions about this but I am a beginner in android and java and I don't how should I solve this problem.

if it is possible for you please answer my question.

thanks for reading my question and sorry if there is any typic problem.

thanks a lot again.

答案1

得分: 1

Question 1: “when and why do we use query?”
Ans: “Query”是从“数据库”检索数据的方式,以便我们可以获取我们存储的数据。我们使用“数据库”来记录并跟踪“用户”等信息。而“查询”只是从“数据库”检索数据的方式。所以每当我们想要从数据库中检索数据或向其添加一些记录时,我们使用“查询”。基本上它是与“数据库”交互的方式。

Question 2: “what does orderByChild method and equalTo method do?”
Ans: “orderByChild”就像对数据进行排序。当我们在Java中使用“Comparators”或“Comparable”时,我们会指定希望它们如何排序,它会根据我们提供的内容进行排序,类似地,“orderByChild("你想要的内容")”会根据它进行排序。
“equalTo()”用于返回具有给定键和值的节点,您可以查看其文档,文档写得很好。

Question 3: “Is query like data snapShot or something like that and what kind of data it holds in itself?”
Ans: 正如在“问题1”中回答的那样,查询是从“数据库”检索记录的一种方式。
当我们编写查询时,我们的目标是从“数据库”中获取特定的记录,有关查询的内容可能存在争议。

Question 5: “Query query = mUserDB.orderByChild("phone").equalTo(mContact.getPhone())” 如果我们没有写“equalTo”会怎样?
Ans: 当我们写上“equalTo(mContact.getPhone())”时,它将在Firebase中检查特定的值,并在找到时返回它。如果您没有写上“equalTo”,它只会为此查询按电话号码排序,然后再返回到其正常形式,不返回任何内容。这意味着当我们发出查询时,它只会按我们想要的方式显示数据,在这里,“orderByChild("phone")”并不实际更改Firebase中的数据外观。

Question 6: “Log.d("print" , query.toString())”
Ans: 我们记录一些内容以跟踪应用程序中发生的情况,有各种log.i||d||v||w||e。
Log.d是用于调试的
DEBUG: 对开发人员有趣的信息,用于在解决问题时进行调试。

英文:

Question 1: when and why do we use query?<br>
Ans: Query is the way to retrieve data from the database, so that we can get are data where we have stored it. We use database to keep records for keeping track of usersetc. And a query is nothing but a way to retrieve data from the database.
So whenever we want to retrieve something from a database or add some records to it we use query. It's basically the way of talking to database <br>

Question 2: what does orderByChild method and equalTo method do?<br>
Ans: orderByChild is just like sorting data. When we use Comparators or Comparable in java we give how do we want them to get sort and it sorts it according to what we feed in, similarly orderByChild(&quot;what you want&quot;) this will sort it according to it.
equalTo() is use to return nodes with the given key and value you can check the documentation for it too, They are well written.<br>

Question 3: Is query like data snapShot or something like that and what kind of data it holds in itself?
Ans: As answered in question 1 Query is a way of retrieving records from the database
When we write a query we aim for getting specific records from the database, It may be controversial about query.<br>

Question 5: Query query = mUserDB.orderByChild(&quot;phone&quot;).equalTo(mContact.getPhone()) What if we didn't write equelTo<br>
Ans: When we write equalTo(mContact.getPhone()) it will check for specific value in the firebase and return it if found. and if you didn't write equalTo it will just order and the phone number for the following query and then again be back to its normal form. and not return anything. That means when we fire a query it just shows us the data as we want And here orderByChild(&quot;phone&quot;) and doesn't actually change the look of it in the firebase.<br>

Question 6: Log.d(&quot;print&quot; , query.toString())
Ans: we log something to keep track of what is happening in our application, there are various log.i||d||v||w||e<br>
Log.d is for debug<br>
DEBUG: Information interesting for Developers, when trying to debug a problem.

huangapple
  • 本文由 发表于 2020年10月8日 16:44:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64258914.html
匿名

发表评论

匿名网友

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

确定