getter 总是 returns 在 android
getter always returns in android
我最近开始学习 Firebase。
向 firebase 添加数据成功,但我在检索
来自 firebase 的数据。
The getter methods which I use for retrieving data always returns
null.
我的MainActivity.java如下:
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;
}
}
我的POJO class如下:
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;
}
}
我的添加项class如下:
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;
}
}
pojos.getListname() 和 pojos.getOwner() 始终为空。
请提供我的问题的解决方案。
提前致谢!
在 MainActivity POJOS pojos=new POJOS();
之后,您没有为 pogos 对象设置任何值,您正试图在设置值之前获取值。尝试在 onDataChange 方法中将 POJOS pojos = new POJOS();
切换为 POJOS pojos = dataSnapshot.getValue(POJOS.class);
。它看起来像这样:
@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());
}
此外,确保 Firebase 中的名称 Mach POJOS class.
尝试如下更改您的 onDataChange(..)
方法:
...
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//POJOS pojos = new POJOS(); // This is create only blank object of POJOS so you are getting null value
// Get POJOS object and use the values to update the UI
POJOS pojos = dataSnapshot.getValue(POJOS.class);
changed_data_view.setText(pojos.getListname());
owner.setText(pojos.getOwner());
Log.i("LIST",pojos.getListname());
Log.i("OWNER",pojos.getOwner());
}
...
侦听器收到 DataSnapshot,其中包含事件发生时数据库中指定位置的数据。在快照 returns 上调用 getValue()
数据的 Java 对象表示。如果该位置不存在数据,则调用 getValue()
returns null
.
请参阅 ValueEventListener 的 Firebase 文档以了解更多信息。
我最近开始学习 Firebase。
向 firebase 添加数据成功,但我在检索 来自 firebase 的数据。
The getter methods which I use for retrieving data always returns null.
我的MainActivity.java如下:
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;
}
}
我的POJO class如下:
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;
}
}
我的添加项class如下:
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;
}
}
pojos.getListname() 和 pojos.getOwner() 始终为空。
请提供我的问题的解决方案。
提前致谢!
在 MainActivity POJOS pojos=new POJOS();
之后,您没有为 pogos 对象设置任何值,您正试图在设置值之前获取值。尝试在 onDataChange 方法中将 POJOS pojos = new POJOS();
切换为 POJOS pojos = dataSnapshot.getValue(POJOS.class);
。它看起来像这样:
@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());
}
此外,确保 Firebase 中的名称 Mach POJOS class.
尝试如下更改您的 onDataChange(..)
方法:
...
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//POJOS pojos = new POJOS(); // This is create only blank object of POJOS so you are getting null value
// Get POJOS object and use the values to update the UI
POJOS pojos = dataSnapshot.getValue(POJOS.class);
changed_data_view.setText(pojos.getListname());
owner.setText(pojos.getOwner());
Log.i("LIST",pojos.getListname());
Log.i("OWNER",pojos.getOwner());
}
...
侦听器收到 DataSnapshot,其中包含事件发生时数据库中指定位置的数据。在快照 returns 上调用 getValue()
数据的 Java 对象表示。如果该位置不存在数据,则调用 getValue()
returns null
.
请参阅 ValueEventListener 的 Firebase 文档以了解更多信息。