Firebase Read getter设置程序 [英] Firebase Read getter setter

查看:44
本文介绍了Firebase Read getter设置程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was trying to get some data from my firebase realtime-database as followed generally, but couldn't figure out where in the code the problem was haunting me.The following Toast statement returns with only the name but it dont print bmdc !Couldn't figure out where the problem was. Can anybody point it out? Anything would be appreciated! The code is attached down below-

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;
}

}

推荐答案

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

The problem lies in the fact that you have an incorrect getter for bmdc field in your model class. The correct getter should be getBmdc() and not getbmdc() as it is in your code now. See the capital letter B versus lower case letter b? The correct model class should look like this:

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客户端将直接在字段上设置该值.但是,如果您想使用设置器,则适合您的字段的设置器是:

The setters are not required, are always optional because if there is no setter for a JSON property, the Firebase client will set the value directly onto the field. But if you wish to use setters, the correct setter for your field is:

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

这篇关于Firebase Read getter设置程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆