sai raman sai raman - 6 months ago 36
Java Question

getter always returns in android

I recently started learning about Firebase.

Adding data to the firebase is successful but I face problems while retrieving the
data from the firebase.


The getter methods which I use for retrieving data always returns
null.


My MainActivity.java is as follows:

public class MainActivityFragment extends Fragment {

TextView time_stamp;


public MainActivityFragment() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView=inflater.inflate(R.layout.fragment_main, container,
false);
final TextView owner=(TextView)rootView.findViewById(R.id.owner);
time_stamp=(TextView)rootView.findViewById(R.id.time_stamp_textView);
final TextView changed_data_view=(TextView)rootView.findViewById(R.id.changedView);

Firebase mRef=new Firebase(Constants.FirebaseUrl).child("activeList");
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

POJOS pojos=new POJOS();
changed_data_view.setText(pojos.getListname());
Log.i("LIST",pojos.getListname());
Log.i("OWNER",pojos.getOwner());

owner.setText(pojos.getOwner());


}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
});

return rootView;
}
}


My POJO class is as follows :

public class POJOS {

public String listname;
public String owner;

public POJOS() {
}

public POJOS(String listname, String owner) {
this.listname = listname;
this.owner = owner;
}

public String getListname(){
return listname;
}

public String getOwner() {
return owner;
}


}


My Add item class is as follows :

public class Dialog extends DialogFragment {


private final String title="ADD AN ITEM";
private EditText editText;
Button addButton;


public Dialog(){

}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().setTitle(title);
View rootView=inflater.inflate(R.layout.dialog_box,null);

editText=(EditText)rootView.findViewById(R.id.editText);
addButton=(Button) rootView.findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userEnteredName=editText.getText().toString();
Firebase firebase=new Firebase(Constants.FirebaseUrl);
POJOS pojos =new POJOS(userEnteredName,"ANONYMOUS USER");

firebase.child("activeList").setValue(pojos);

dismiss();



}
});



return rootView;
}

}


The pojos.getListname() and pojos.getOwner() are always null.

Please provide a solution to my problem.

Thanks in advance !

Answer

In your MainActivity after POJOS pojos=new POJOS(); you didn't set any values to the pogos object, you are trying to get a value before you set the value. Try to switch POJOS pojos = new POJOS(); with POJOS pojos = dataSnapshot.getValue(POJOS.class); in the onDataChange method. It will look like this:

@Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            POJOS pojos = dataSnapshot.getValue(POJOS.class);
            changed_data_view.setText(pojos.getListname());
            Log.i("LIST",pojos.getListname());
            Log.i("OWNER",pojos.getOwner());
            owner.setText(pojos.getOwner());
        }

In addition, make sure that the names in Firebase Mach the names in the POJOS class.