【问题标题】:data not retrieving from firebase未从 firebase 检索数据
【发布时间】:2017-02-22 12:09:04
【问题描述】:

我正在尝试从 Firebase 检索数据,但在将 valueEventListener 应用于数据后显示空数据,ShoppingList 类 userShopping 中包含的值抛出空

Firebase 数据快照:

值事件监听器:

  mActiveValueListener = mActiveDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    ShoppingList shoppingList=dataSnapshot.getValue(ShoppingList.class);
    Log.e(LOG_TAG,"is shopping list null:"+String.valueOf(shoppingList==null));
    if (shoppingList==null) {
        finish(); 
        return;
    }
    mShoppingList=shoppingList;

    mCurrentUserIsOwner= Utils.checkIfOwner(shoppingList,mEncodedEmail);

    Log.e(LOG_TAG,"Owner :"+String.valueOf(mCurrentUserIsOwner));
    invalidateOptionsMenu();
    setTitle(shoppingList.getListName());
    mActiveListItemAdapter.setShoppingList(mShoppingList);
    HashMap<String,Users> usersShopping=mShoppingList.getUserShopping();

    Log.e(LOG_TAG,"User is null: "+String.valueOf(usersShopping==null));


    if (usersShopping!=null && usersShopping.size()!=0 && usersShopping.containsKey(mEncodedEmail)){
        Log.e(LOG_TAG,"user shopping on");
        mShopping=true;
        mButtonShopping.setText(getString(R.string.button_stop_shopping));
        mButtonShopping.setBackgroundColor(ContextCompat.getColor(ActiveListDetailsActivity.this,R.color.dark_grey));
    }
    else{
        Log.e(LOG_TAG,"user shopping off");
        mButtonShopping.setText(getString(R.string.button_start_shopping));
        mButtonShopping.setBackgroundColor(ContextCompat.getColor(ActiveListDetailsActivity.this,R.color.primary_dark));
        mShopping=false;
    }

}

ShoppingList类:

public class ShoppingList {
    private String listName;
    private String owner;
    private HashMap<String, Object> timestampLastChanged;
    private HashMap<String,Users> userShopping;

    /**
     * Required public constructor
     */
    public ShoppingList() {
    }

    /**
     * Use this constructor to create new ShoppingLists.
     * Takes shopping list listName and owner. Set's the last
     * changed time to what is stored in ServerValue.TIMESTAMP
     *
     * @param listName
     * @param owner
     *
     */
    public ShoppingList(String listName, String owner, HashMap<String,Object> timestampLastChanged) {
        this.listName = listName;
        this.owner = owner;
        HashMap<String, Object> timestampLastChangedObj = new HashMap<String, Object>();
        timestampLastChangedObj.put(Constants.FIREBASE_PROPERTY_TIMESTAMP, ServerValue.TIMESTAMP);
        this.timestampLastChanged = timestampLastChangedObj;
        this.userShopping=new HashMap<String,Users>();

    }

    public String getListName() {
        return listName;
    }

    public String getOwner() {
        return owner;
    }


    public HashMap<String, Object> getTimestampLastChanged() {
        return timestampLastChanged;
    }


    @Exclude
    public long getTimestampLastChangedLong() {

        return (long) timestampLastChanged.get(Constants.FIREBASE_PROPERTY_TIMESTAMP);
    }

    public HashMap<String, Users> getUserShopping() {
        return userShopping;
    }
}

日志:

Users类:

public class Users {
    private String email;
    private String name;
    private HashMap<String,Object> timestampJoined;
    private boolean hasLoggedInWithPassword;

    public Users(){}

    public Users(String email,String name,HashMap<String,Object> timestampJoined)
    {
        this.email=email;
        this.name=name;
        this.timestampJoined=timestampJoined;
        this.hasLoggedInWithPassword=false;
    }

    public String getName() {
        return name;
    }

    public HashMap<String, Object> getTimestampJoined() {
        return timestampJoined;
    }

    public String getEmail() {
        return email;
    }

    public boolean isHasLoggedInWithPassword() {
        return hasLoggedInWithPassword;
    }
}

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    用这个方法

    ShoppingList shoppingList=dataSnapshot.getValue(ShoppingList.class);
    

    Firebase 调用了空构造函数

    public ShoppingList() {
    }
    

    所以你的字段userShopping 为空,使mShoppingList.getUserShopping() 也为空。

    如果您希望 Firebase 分配字段,我认为您需要 getter setter。

    (如果该数据不在 Firebase 模型中,您仍然会得到 null)

    【讨论】:

    • 感谢您的回答。 shoppingList 不为空,您可以在 Log.e 图像中看到它
    • 不是那个...User is null: "+String.valueOf(usersShopping==null))
    • 我添加了这个 public void setUserShopping(HashMap userShopping) { this.userShopping = userShopping; }
    • 仍然抛出null
    • 你能在问题中添加用户类吗?您是否能够将任何用户对象设为非空?
    【解决方案2】:

    在您的 ShoppingList 类中进行以下更改

    private HashMap<String,Users> usersShopping;
    public ShoppingList(String listName, String owner, HashMap<String,Object> timestampLastChanged) {
        //...
        this.usersShopping=new HashMap<String,Users>();
    
    }
    
    public HashMap<String, Users> getUsersShopping() {
        return usersShopping;
    }
    

    最好在课堂内(windows)

    (CTRL + A) + (CTRL + R) [userShopping, usersShopping] -> [Replace All] 
    

    【讨论】:

    • 谢谢你的回答,我也试过这个方法,问题没有解决。
    【解决方案3】:

    如果你想检索所有的购物清单,你应该循环抛出快照,这是你这样做的:

    public void onDataChange(DataSnapshot snapshot) {
    for (DataSnapshot postSnapshot : snapshot.getChildren()) {
     //Getting the data from snapshot 
    ShoppingList  ShoppingList = postSnapshot.getValue(Person.class); 
        //Adding it to a string 
        String string = "Name: 
          "+shoppingList.getName()+"\nEmail: "+shoppingList.getEmail()+"\n\n";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      相关资源
      最近更新 更多