java - 如何从firebase数据库中以文本形式显示我的数据实时我的代码和我的XML在
问题描述
我做了这个项目,我无法显示我的数据并在 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>
我无法解决这个问题,每次我运行它时它都不是可用的吐司。
解决方案
您在查询中有错误。而不是这个
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)");
使用查询毫无意义并且不知道节点的任何数据
推荐阅读
- firebase - 由于我的应用程序使用密码身份验证,因此上面的代码将在该字段中给我 null
- azure - 如何在 Azure 托管应用程序 ARM 部署中强制选择服务主体
- c# - 如何仅获取 C# 中两个 DateTimeOffset 之间的时间差,而忽略日期差?
- c++ - std::mutex 的 constexpr 构造函数是如何实现的?
- firebase - Firebase 身份验证 Google 登录以仅允许特定的 google 工作区帐户
- linux - X11 监听嵌套窗口的光标样式更新(chrome)
- c++ - GetProcAddress 在 DLL 中找不到函数
- javascript - innerText 导致 [object HTMLLIElement] 而不是 li 文本
- html - 我怎样才能让我的网站响应?我正在使用 glitch.com
- java - 如何使用 postman 在 Java 中传递 JSONObject 类型值