从Firebase数据库中检索数据,在Android中使用Java通过ID进行搜索。

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

Retrieve data from FireBase database in android, java by searchin with an ID

问题

我尝试在Java中实现了一段代码,用于从数据库(Firebase)检索数据,通过搜索一个ID,并在表单中显示它。

btnShow.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        searchID = txtid.getText().toString().trim();
        DatabaseReference readRef = FirebaseDatabase.getInstance().getReference();
        DatabaseReference dref = readRef.child("Student");
        Query query = dref.orderByChild("id").equalTo(searchID);
        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(dataSnapshot.hasChildren()){
                    txtID.setText((dataSnapshot.child("id").getValue().toString()));
                    txtName.setText((dataSnapshot.child("name").getValue().toString()));
                    txtAdd.setText((dataSnapshot.child("address").getValue().toString()));
                    txtConNo.setText((dataSnapshot.child("conNo").getValue().toString()));
                }
                else
                    Toast.makeText(getApplicationContext(),"No Source to Display",Toast.LENGTH_SHORT).show();

            }

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

            }
        });
    }
});

这是我实现的Java代码。但是当我运行程序并在数据库中提供正确的ID时,它总是显示为"没有要显示的数据源"(我的数据库中有数据,并且保存功能正常工作)。

我在Android Studio和Firebase方面是初学者。请修改上述代码,告诉我如何按ID搜索。

谢谢。

英文:

I tried to implement a code in java to retrieve data from database( firebase) by searching with an ID and show it in the form.

从Firebase数据库中检索数据,在Android中使用Java通过ID进行搜索。
In here i have already saved some data and now when i type the id, it need to show the data of the id in this same form.

btnShow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            searchID = txtid.getText().toString().trim();
            DatabaseReference readRef = FirebaseDatabase.getInstance().getReference();
            DatabaseReference dref = readRef.child("Student");
            Query query = dref.orderByChild("id").equalTo(searchID);
            query.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if(dataSnapshot.hasChildren()){
                        txtID.setText((dataSnapshot.child("id").getValue().toString()));
                        txtName.setText((dataSnapshot.child("name").getValue().toString()));
                        txtAdd.setText((dataSnapshot.child("address").getValue().toString()));
                        txtConNo.setText((dataSnapshot.child("conNo").getValue().toString()));
                    }
                    else
                        Toast.makeText(getApplicationContext(),"No Source to Display",Toast.LENGTH_SHORT).show();

                }

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

                }
            });
        }
    });

This is the java code i implemented to do that. But when i run the program and give the correct id in database it is always showing as "No source to display"( There are data in my database and save function is working properly)

从Firebase数据库中检索数据,在Android中使用Java通过ID进行搜索。

I am a beginner in android studio and firebase. Please modify the above code and tell me how can i search by ID.

Thankyou.

答案1

得分: 2

请看以下内容:

if(dataSnapshot.exists()){
    for(DataSnapshot ds : dataSnapshot.getChildren()){
       txtID.setText((ds.child("id").getValue().toString()));
       txtName.setText((ds.child("name").getValue().toString()));
       txtAdd.setText((ds.child("address").getValue().toString()));
       txtConNo.setText((ds.child("conNo").getValue().toString()));
    }
}

您的参考节点位于Student,因此您需要循环然后使用ds来访问属性。另外,在onCancelled内部不要忘记检查错误:

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
    Log.d("TAG", databaseError.getMessage()); //不要忽略潜在错误!
}
英文:

Try the following:

if(dataSnapshot.exists()){
    for(DataSnapshot ds : dataSnapshot.getChildren()){
       txtID.setText((ds.child("id").getValue().toString()));
       txtName.setText((ds.child("name").getValue().toString()));
       txtAdd.setText((ds.child("address").getValue().toString()));
       txtConNo.setText((ds.child("conNo").getValue().toString()));
    }
}

Your reference is at node Student therefore you need to loop and then use ds to access the attributes. Also, don't forget to check for errors inside onCancelled:

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
    Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
}

huangapple
  • 本文由 发表于 2020年9月16日 22:52:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63922671.html
匿名

发表评论

匿名网友

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

确定