Firebase Read getter setter

回眸只為那壹抹淺笑 提交于 2019-12-25 02:16:17

问题


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

}

}

i used getter setter ..

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

}


回答1:


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

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


来源:https://stackoverflow.com/questions/51423501/firebase-read-getter-setter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!