Firebase Read getter设置程序

时间:2018-07-19 13:07:48

标签: android firebase firebase-realtime-database

我试图按照常规方法从firebase实时数据库中获取一些数据,但是无法弄清楚问题出在哪里,下面的Toast语句仅返回名称,但不输出bmdc !无法找出问题所在。有人可以指出吗?任何事情将不胜感激!该代码附在下面-

package 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.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
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;

import org.w3c.dom.Text;

public class  FindDocActivity extends AppCompatActivity {

private ImageView SearchButton_doc;
private EditText SearchInput_doc;

private RecyclerView SearchResult_doc;

private DatabaseReference allDocdatabaseref;

FirebaseRecyclerAdapter<FindDoctors, FindDoctorsViewHolder>  firebaseRecyclerAdapter;

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

    SearchResult_doc = (RecyclerView) findViewById(R.id.search_result_list1);
    SearchResult_doc.setHasFixedSize(true);
    SearchResult_doc.setLayoutManager(new LinearLayoutManager(this));

    SearchButton_doc = (ImageView) findViewById(R.id.search_people_btn1);
    SearchInput_doc = (EditText) findViewById(R.id.Search_box_input1);

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


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

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

            SearchDoc(searchBoxInput1);


        }
    });


}

private void SearchDoc(String searchBoxInput1) {
    Toast.makeText(this, "searching..", Toast.LENGTH_LONG).show();

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


    firebaseRecyclerAdapter
            = new FirebaseRecyclerAdapter<FindDoctors, FindDoctorsViewHolder>(
            FindDoctors.class,
            R.layout.all_userdoc_display_layout,
            FindDoctorsViewHolder.class,
            searchdoctorquery

    ) {
        @Override
        protected void populateViewHolder(FindDoctorsViewHolder viewHolder, FindDoctors model, int position) {
            /// Toast.makeText(getApplicationContext(),model.getName().toString(), Toast.LENGTH_LONG).show();

            viewHolder.setbmdc(model.getbmdc());
            viewHolder.setName(model.getName());
        }
    };
    SearchResult_doc.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.startListening();
}

public static class FindDoctorsViewHolder extends RecyclerView.ViewHolder {
    View mView;

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

    public void setbmdc(String bmdc) {

        TextView Docbmdc = mView.findViewById(R.id.all_user_profile_name1);
        Docbmdc.setText(bmdc);
    }

    public void setName(String name) {
        TextView Docname = mView.findViewById(R.id.all_user_profile_status1);
        Docname.setText(name);
    }

}

}

我用吸气剂塞特..

package com.example.abed.smit;

public class FindDoctors {

public String bmdc , name;

public FindDoctors()
{

}
public FindDoctors(String bmdc, String name) {
    this.bmdc =bmdc;
    this.name = name;
}

public String getbmdc() {
    return bmdc;
}

public void setbmdc(String bmdc) {
    this.bmdc = bmdc;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

here is the result!

Database

1 个答案:

答案 0 :(得分:0)

问题在于,您的模型类中的bmdc字段的getter不正确。正确的getter应该是getBmdc(),并且不是 getbmdc(),就像现在在您的代码中一样。看到大写字母B和小写字母b吗?正确的模型类应如下所示:

public class FindDoctors {
    private String bmdc, name;

    public FindDoctors() {}

    public FindDoctors(String bmdc, String name) {
        this.bmdc = bmdc;
        this.name = name;
    }

    public String getBmdc() { return bmdc; }

    public String getName() { return name; }
}

设置程序不是必需的,它们始终是可选的,因为如果JSON属性没有设置程序,Firebase客户端将直接在字段上设置值。但是,如果您想使用设置器,则适合您的字段的设置器是:

public void setBmdc(String bmdc) {
    this.bmdc = bmdc;
}