android - 如何从 Firebase android获取所有子列表

  显示原文与译文双语对照的内容
144 1

我想从Firebase中的所有子列表。

我已经实现了这个代码,但它不工作。

mFirebaseRef = new Firebase(FIREBASE_URL);
mFirebaseRef.addValueEventListener(new ValueEventListener() {
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
 List<String> td = (ArrayList<String>) dataSnapshot.getValue();
//notifyDataSetChanged();
 }
 @Override
 public void onCancelled(FirebaseError firebaseError) {
 }
});
时间:原作者:0个回答

110 4

我希望下面的代码能正常工作

Firebase ref = new Firebase(FIREBASE_URL);
 ref.addValueEventListener(new ValueEventListener() {
 @Override
 public void onDataChange(DataSnapshot snapshot) {
 Log.e("Count",""+snapshot.getChildrenCount());
 for (DataSnapshot postSnapshot: snapshot.getChildren()) {
 <YourClass> post = postSnapshot.getValue(<YourClass>.class);
 Log.e("Get Data", post.<YourMethod>());
 }
 }
 @Override
 public void onCancelled(FirebaseError firebaseError) {
 Log.e("The read failed:", firebaseError.getMessage());
 }
 });
原作者:
68 2

Firebase以以下格式存储一系列值:

"-K-Y_Rhyxy9kfzIWw7Jq":"Value 1"
"-K-Y_RqDV_zbNLPJYnOA":"Value 2"
"-K-Y_SBoKvx6gAabUPDK":"Value 3"

如果你是这样做的你就会得到错误的类型。上述结构表示为 Map,而不是 List:

mFirebaseRef = new Firebase(FIREBASE_URL);
mFirebaseRef.addValueEventListener(new ValueEventListener() {
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
 Map<String, Object> td = (HashMap<String,Object>) dataSnapshot.getValue();
 List<Object> values = td.values();
//notifyDataSetChanged();
 }
 @Override
 public void onCancelled(FirebaseError firebaseError) {
 }
});
原作者:
126 3
FirebaseDatabase mFirebaseDatabase = FirebaseDatabase.getInstance();
 DatabaseReference databaseReference = mFirebaseDatabase.getReference(FIREBASE_URL);
 databaseReference.addValueEventListener(new ValueEventListener() {
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
 for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
 Log.v(TAG,""+ childDataSnapshot.getKey());//displays the key for the node
 Log.v(TAG,""+ childDataSnapshot.child(--ENTER THE KEY NAME eg. firstname or email etc.--).getValue());//gives the value for given keyname
 }
 }
 @Override
 public void onCancelled(DatabaseError databaseError) {
 }
 });

希望有帮助!

原作者:
110 4

我做了这样的事情:

Firebase ref = new Firebase(FIREBASE_URL);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
 Map<String, Object> objectMap = (HashMap<String, Object>) 
 dataSnapshot.getValue();
 List<Match> = new ArrayList<Match>();
 for (Object obj : objectMap.values()) {
 if (obj instanceof Map) {
 Map<String, Object> mapObj = (Map<String, Object>) obj;
 Match match = new Match();
 match.setSport((String) mapObj.get(Constants.SPORT));
 match.setPlayingWith((String) mapObj.get(Constants.PLAYER));
 list.add(match);
 }
 }
 }
 @Override
 public void onCancelled(FirebaseError firebaseError) {
 }
});
原作者:
50 5

Frank说,英镑"key":"Value"的格式存储值序列,这是一个收费的Map 结构

要从这个序列中获得清单

  • 用英镑 HashMap HashMap英镑 HashMap HashMap和你的英镑对象初始化英镑 GenericTypeIndicator 。
  • 获取英镑DataSnapShot英镑的值,以英镑为单位的地图 。
  • 初始化带有英镑 HashMap HashMap值的 ArrayList ArrayList 。
  • GenericTypeIndicator<HashMap<String, Object>> objectsGTypeInd = new GenericTypeIndicator<HashMap<String, Object>>() {};
    Map<String, Object> objectHashMap = dataSnapShot.getValue(objectsGTypeInd);
    ArrayList<Object> objectArrayList = new ArrayList<Object>(objectHashMap.values());

对我工作很正常希望能帮助。

原作者:
92 1

在我的案例中只有给定的解决方案才能正常工作。

Firebase ArrayList结构的屏幕截图:

enter image description here

如何从英镑收取全部清单从英镑 DataSnapshot 。

GenericTypeIndicator<Map<String, List<Education>>> genericTypeIndicator = new GenericTypeIndicator<Map<String, List<Education>>>() {};
Map<String, List<Education>> hashMap = dataSnapshot.getValue(genericTypeIndicator);
for (Map.Entry<String,List<Education>> entry : hashMap.entrySet()) {
 List<Education> educations = entry.getValue();
 for (Education education: educations){
 Log.i(TAG, education.Degree);
 }
}

Education.java: ( 模型类) 。

public class Education implements Serializable{
 public String Degree;
 public String Result;
}

希望这能正常工作。

原作者:
140 4

保存和Retriving数据到 Firebase ( 已弃用 2.4.2 )

Firebase fb_parent = new Firebase("YOUR-FIREBASE-URL/");
Firebase fb_to_read = fb_parent.child("students/names");
Firebase fb_put_child = fb_to_read.push();//REMEMBER THIS FOR PUSH METHOD
//INSERT DATA TO STUDENT - NAMES I Use Push Method
fb_put_child.setValue("Zacharia");//OR fb_put_child.setValue(YOUR MODEL) 
fb_put_child.setValue("Joseph");//OR fb_put_child.setValue(YOUR MODEL) 
fb_put_child.setValue("bla blaaa");//OR fb_put_child.setValue(YOUR MODEL) 
//GET DATA FROM FIREBASE INTO ARRAYLIST
fb_to_read.addValuesEventListener....{
 public void onDataChange(DataSnapshot result){
 List<String> lst = new ArrayList<String>();//Result will be holded Here
 for(DataSnapshot dsp : result.getChildren()){
 lst.add(String.valueOf(dsp.getKey()));//add result into array list
 }
//NOW YOU HAVE ARRAYLIST WHICH HOLD RESULTS
for(String data:lst){ 
 Toast.make(context,data,Toast.LONG_LENGTH).show; 
 }
 }
}
原作者:
86 1

Works a

final DatabaseReference senderDb = FirebaseDatabase.getInstance().getReference(Constant.NODE_MESSAGE).child(myId +"_" + otherId);
 senderDb.addValueEventListener(new ValueEventListener() {
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
 Map<String, Object> td = (HashMap<String,Object>) dataSnapshot.getValue();
 for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
 DatabaseReference objRef = senderDb.child( childDataSnapshot.getKey());
 Map<String,Object> taskMap = new HashMap<String,Object>();
 taskMap.put("is_read","1");
 objRef.updateChildren(taskMap);//should I use setValue()...?
 Log.v("Testing",""+ childDataSnapshot.getKey());//displays the key for the node
 }
//notifyDataSetChanged();
 }
 @Override
 public void onCancelled(DatabaseError databaseError) {
 }
 });
原作者:
136 3
mDatabase.child("token").addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
 for (DataSnapshot snapshot:dataSnapshot.getChildren())
 {
 String key= snapshot.getKey();
 String value=snapshot.getValue().toString();
 }
 }
 @Override
 public void onCancelled(DatabaseError databaseError) {
 Toast.makeText(ListUser.this,databaseError.toString(),Toast.LENGTH_SHORT).show();
 }
 });

仅在子进程没有 SubChild 时工作

enter image description here

原作者:
146 3

你需要编写一个定制的反序列化器然后循环它,得到hasmap的值。

自定义解串器:-

public class UserDetailsDeserializer implements JsonDeserializer<AllUserDetailsKeyModel> {
/*
 bebebejunskjd:{
"email":"akhilbv1@gmail.com",
"mobileNum":"12345678",
"password":"1234567",
"username":"akhil"}*/
 @Override public AllUserDetailsKeyModel deserialize(JsonElement json, Type typeOfT,
 JsonDeserializationContext context) throws JsonParseException {
 final JsonObject jsonObject = json.getAsJsonObject();
 Gson gson = new Gson();
 Type AllUserDetailsResponseModel =
 new TypeToken<HashMap<String, AllUserDetailsResponseModel>>(){}.getType();
 HashMap<String, AllUserDetailsResponseModel> user =
 gson.fromJson(jsonObject, AllUserDetailsResponseModel);
 AllUserDetailsKeyModel result = new AllUserDetailsKeyModel();
 result.setResult(user);
 return result;
 }
}

注释中的代码是我的对象模型,应该将replaceAllUserDetailsKeyModel与模型类一起使用,并将它的添加到下面的rest客户机中:-

private Converter.Factory createGsonConverter() {
 GsonBuilder gsonBuilder = new GsonBuilder();
 gsonBuilder.registerTypeAdapter(AllUserDetailsKeyModel.class, new UserDetailsDeserializer());
 Gson gson = gsonBuilder.create();
 return GsonConverterFactory.create(gson);
 }

这是用于改造的定制转换器。

在onResponse中,只使用hasmaps循环,并按键获取值,我的模型类如下所示:-

public class AllUserDetailsKeyModel {
 private Map<String, AllUserDetailsResponseModel> result;
 public Map<String, AllUserDetailsResponseModel> getResult() {
 return result;
 }
 public void setResult(Map<String, AllUserDetailsResponseModel> result) {
 this.result = result;
 }
}

你可能需要给出类型T,其中的数据类型和模型只包含一个hashmap和getter和 setter 。

并最终设置定制转换器的改造如下:-.addConverterFactory(createGsonConverter())

如果需要更详细的说明,请告诉我。

原作者:
...