【问题标题】:getter always returns in androidgetter 总是在 android 中返回
【发布时间】:2016-05-29 22:29:47
【问题描述】:

我最近开始了解 Firebase。

将数据添加到 Firebase 成功,但我在检索数据时遇到了问题 来自 firebase 的数据。

我用于检索数据的 getter 方法总是返回 空。

我的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 类如下:

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


}

我的添加项目类如下:

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() 始终为 null。

请提供我的问题的解决方案。

提前致谢!

【问题讨论】:

  • 确保 Firebase 中的名称与 POJOS 类中的名称一致。
  • 在你的 MainActivity 之后 POJOS pojos=new POJOS(); 你没有为 pogos 对象设置任何值,你试图在你之前获得一个值设置值。先试试POJOS pojos = dataSnapshot.getValue(POJOS.class);

标签: java android firebase getter firebase-realtime-database


【解决方案1】:

尝试如下更改您的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,其中包含事件发生时数据库中指定位置的数据。对快照调用getValue() 会返回数据的Java 对象表示。如果该位置不存在数据,则调用getValue() 将返回null

请参阅 Firebase 文档以获取 ValueEventListener 以了解更多信息。

【讨论】:

    【解决方案2】:

    POJOS pojos=new POJOS(); 之后的 MainActivity 中,您没有为 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 中的名称与 POJOS 类中的名称一致。

    【讨论】:

      猜你喜欢
      • 2020-01-26
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      相关资源
      最近更新 更多