首页 > 解决方案 > 如何从firebase数据库中以文本形式显示我的数据实时我的代码和我的XML在

问题描述

我做了这个项目,我无法显示我的数据并在 firebase 中搜索序列号需要任何帮助才能知道问题出在哪里。我尝试并尝试观看所有firebase教程,但找不到答案。

我在 Firebase 中保存数据如下:

我的 Firebase 数据库

这是java代码:

public class Search_For_lost extends AppCompatActivity {

    TextView search_input;
    Button search_btn;
    TextView result;
    DatabaseReference reff;
    Query query;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search__for_lost);

        search_input=(EditText)findViewById(R.id.search_input);
        result=(TextView) findViewById(R.id.Result_list_view);
        search_btn=(Button)findViewById(R.id.search_button);
        reff= FirebaseDatabase.getInstance().getReference().child("Database").child("Users");
        search_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                reff= FirebaseDatabase.getInstance().getReference().child("Database").child("Users");
                Query query=reff.child("Database").orderByChild("Users").equalTo("serial");
                query.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        if (snapshot.exists()){
                            String username=snapshot.child("name").getValue().toString();
                            String serial=snapshot.child("serial").getValue().toString();
                            Toast.makeText(getApplicationContext(),username+ ""+serial, Toast.LENGTH_SHORT).show();
                        }
                        else {
                            Toast.makeText(Search_For_lost.this, "Serial is not avaliable", Toast.LENGTH_SHORT).show();
                        }
                    }

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

                    }
                });

这是我的 XML 布局文件:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Search_For_lost">

        <EditText
            android:id="@+id/search_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/input_design"
            android:layout_marginRight="20dp"
            android:layout_marginLeft="20dp"
            android:hint="Search for Serial Number.."
            android:textSize="22sp"
            android:padding="15dp"
            android:layout_marginTop="100dp"
            android:gravity="center" />
            
        <Button
                android:id="@+id/search_button"
                android:layout_width="match_parent"
                android:background="@drawable/buttons"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dp"
                android:layout_marginRight="25dp"
                android:gravity="center"
                android:text="Search.."
                android:textSize="25sp"
                android:layout_below="@+id/search_input"
                android:layout_marginTop="70dp"/>

        <TextView
            android:id="@+id/Result_list_view"
            android:layout_width="match_parent"
            android:textSize="20dp"
            android:gravity="center"
            android:layout_height="350dp"
            android:layout_below="@+id/search_button"
            android:layout_marginTop="30dp"/>
    
    </RelativeLayout>

我无法解决这个问题,每次我运行它时它都不是可用的吐司。

标签: javaandroidfirebasefirebase-realtime-database

解决方案


您在查询中有错误。而不是这个

reff= FirebaseDatabase.getInstance().getReference().child("Database").child("Users");
Query query=reff.child("Database").orderByChild("Users").equalTo("serial");

用这个

reff= FirebaseDatabase.getInstance().getReference().child("Database").child("Users");
Query query=reff.orderByChild("serial").equalTo("(here put the serial that you are searching about)");

使用查询毫无意义并且不知道节点的任何数据


推荐阅读