2016-12-01 37 views
-1

我有這個問題,我無法從我的firebase json獲取單個值,並檢查它是否爲null。Android Firebase獲得單個值作爲字符串

這裏是我的JSON文件:

{ "117500978158564407389": 
    { 
    "uid" : "117500978158564407389", 
    "cat_list" : { 
    "category_list" : [ { 
     "cat_name" : "Work" 
    }, { 
     "cat_name" : "Health" 
    }, { 
     "cat_name" : "Food" 
    }, { 
     "cat_name" : "Spare time" 
    }, { 
     "cat_name" : "Travel" 
    } ] 
    } 
    } 
    } 

這是我在MVP的演示代碼,我嘗試在JSON讓我的「UID」值。但它始終顯示爲空。

@Override 
public String getId(String id) {  
    reference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://talisproject-89e62.firebaseio.com/userData/"); 
    reference.child("117500978158564407389").addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 

      CategoryList categoryList = dataSnapshot.getValue(CategoryList.class); 
      ID = categoryList.getUid(); 

     } 
     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 

    return ID; 
} 
} 

這裏是我的POJO類

public class CategoryList implements Serializable { 
public List<Category> categoryList; 


public List<Category> getCategoryList() { 
    return categoryList; 
} 

public void setCategoryList(List<Category> categoryList) { 
    this.categoryList = categoryList; 
} 

public CategoryList catList; 
public String uid; 

public String getUid() { 
    return uid; 
} 

public void setUid(String uid) { 
    this.uid = uid; 
} 

public CategoryList(CategoryList catList, String uid) { 

    this.catList = catList; 
    this.uid = uid; 
} 

} 

,在這裏,我打電話給我演示功能,它總是顯示。有誰知道,爲什麼它總是顯示一個null,我怎麼能得到這個值?

ID = interPresenter.getId(uid); 
+4

*有誰知道,爲什麼它總是顯示一個空* ...我知道...火力是異步http://ideone.com/PPHi95 ......顯然'返回ID;'出現在聽衆點擊'onDataChange'之前 – Selvin

回答