Firebase数据检索geter错误

时间:2018-07-21 08:07:05

标签: java android firebase firebase-realtime-database

我试图按照常规方法从firebase实时数据库中获取一些数据,但无法弄清楚问题出在哪里。无法找出问题出在哪里。有人可以指出吗?任何事情将不胜感激!该代码附在下面-     `包com.example.abed.smit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

public class FindMedicineActivity extends AppCompatActivity {

private Button SearchButton_medicine;
private EditText SearchInput_medicine;

private RecyclerView SearchResult_medicine;

private DatabaseReference allDocdatabaseref;

FirebaseRecyclerAdapter<FindMedicine,FindMedicineViewHolder> firebaseRecyclerAdapter;

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

    SearchResult_medicine = (RecyclerView) findViewById(R.id.search_result_list2);
    SearchResult_medicine.setHasFixedSize(true);
    SearchResult_medicine.setLayoutManager(new LinearLayoutManager(this));

    SearchButton_medicine =  findViewById(R.id.search_people_btn2);
    SearchInput_medicine = (EditText) findViewById(R.id.Search_box_input2);

    allDocdatabaseref = FirebaseDatabase.getInstance().getReference().child("MEDICINE");


    SearchButton_medicine.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String searchBoxInput1 = SearchInput_medicine.getText().toString().trim();

            SearchMedicine(searchBoxInput1);


        }
    });
}

private void SearchMedicine(String searchBoxInput1) {

    Toast.makeText(this, "searching..", Toast.LENGTH_LONG).show();

    Query searchmedicinequery = allDocdatabaseref.orderByChild("medicinename").startAt(searchBoxInput1).endAt(searchBoxInput1 + "\uf8ff");

    firebaseRecyclerAdapter
            = new FirebaseRecyclerAdapter<FindMedicine, FindMedicineViewHolder>(
            FindMedicine.class,
            R.layout.all_medicine_display_layout,
            FindMedicineViewHolder.class,
            searchmedicinequery

    ) {
        @Override
        protected void populateViewHolder(FindMedicineViewHolder viewHolder, FindMedicine model, int position) {
             viewHolder.setMedicinename(model.getMedicinename());
        }
    };
    SearchResult_medicine.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.startListening();
}

private class FindMedicineViewHolder extends RecyclerView.ViewHolder {
    View mView;

    public FindMedicineViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }



    public void setMedicinename(String medicinename) {
        TextView Docname = mView.findViewById(R.id.all_user_profile_name2);
        Docname.setText(medicinename);
    }
}

}

我也使用吸气剂..

package com.example.abed.smit;

public class FindMedicine {

public String medicinename;

public FindMedicine(String medicinename) {
    this.medicinename = medicinename;
}

public String getMedicinename() {
    return medicinename;
}

}

database image !!! here

xml所有药物

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#000000">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="5dp">

    <TextView
        android:id="@+id/all_user_profile_name2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="UserFullNAme"
        android:textColor="#91DC5A"
        android:textSize="18sp" android:textStyle="bold"/>


</LinearLayout>


</LinearLayout>

find_medicine 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"
android:background="#000000"
tools:context=".FindMedicineActivity">


<RelativeLayout
    android:id="@+id/myLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true">


    <EditText
        android:id="@+id/Search_box_input2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:drawableLeft="@drawable/ic_action_name"
        android:hint="Enter Bmdc Number"
        android:textColor="#FFFFFFFF"
        android:textColorHint="#FFFFFFFF"
        android:textSize="16sp"
        android:textStyle="bold" />


    <Button
        android:id="@+id/search_people_btn2"
        android:layout_width="170dp"
        android:layout_height="30dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="94dp"
        android:layout_marginTop="67dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:text="Confirm"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="94dp" />

</RelativeLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/search_result_list2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/myLayout2"
    android:layout_margin="10dp"
    android:background="#000000"

    >

</android.support.v7.widget.RecyclerView>

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

我想这是访问问题,现在我认为您的应用程序中没有身份验证。所以我想这是数据库规则问题。

检查实时数据库中的“规则”标签,然后执行此操作(如果不是):

".read":"true"

答案 1 :(得分:1)

将查询更改为-

Query searchmedicinequery = allDocdatabaseref.orderByChild("name").startAt(searchBoxInput1).endAt(searchBoxInput1 + "\uf8ff");

根据firebase文档- 该类具有一个空的构造函数,这对于Firebase的自动数据映射是必需的。

因此将模型更改为-

public class FindMedicine {

    public String name;

    public FindMedicine() {
    }

    public FindMedicine(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}