我尝试从Firebase数据库下载聊天数据(数据结构如下,建议使用Firebase官方网站)。
"chats": {
"one": {
"lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
"timestamp": 1459361875666
},
"two": { ... }
},
"members": {
"one": {
"member1": "cxgF3EddyMWiYybXdMsYwu5YQsn1",
"member2": "jdxBDm5MXBdX4zfGoEp9hgHqscn2"
};
"two": {
"member1": "cxgF3EddyMWiYybXdMsYwu5YQsn1",
"member2": "yGpTEPDfChVmUeEkzvCCY6IjQnA2"
};
},
"messages": {
"one": {
"m1": {
"name": "cxgF3EddyMWiYybXdMsYwu5YQsn1",
"message": "The relay seems to be malfunctioning.",
"timestamp": 1459361875337
},
"m2": {
"name": "jdxBDm5MXBdX4zfGoEp9hgHqscn2",
"message": "Yes.",
"timestamp": 1459361875689
},
},
"two": { ... }
}
现在,我正在尝试检查我(作为登录用户)是否参与了对话并仅下载聊天/特定聊天号码下的最后一条消息。此外,我必须下载此聊天中涉及的另一个用户的个人资料图片,然后将最后一条消息和此图片添加到PrefenceScreen。 这是代码:
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("members");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final ArrayList<String> arrayList = new ArrayList<>();
for (final DataSnapshot snapshot : dataSnapshot.getChildren()) {
GenericTypeIndicator<Map<String, Object>> m = new GenericTypeIndicator<Map<String, Object>>() {
};
Map<String, Object> map = snapshot.getValue(m);
if (map.get("member1").equals(myUID)) {
arrayList.add(snapshot.getKey());
arrayList.add(map.get("member2").toString());
} else if (map.get("member2").equals(myUID)) {
arrayList.add(snapshot.getKey());
arrayList.add(map.get("member1").toString());
}
}
mStorage = FirebaseStorage.getInstance().getReference();
for (int i=0, y=1; y<arrayList.size(); i+=2, y+=2) {
Log.i("Chat", arrayList.get(i));
StorageReference storageReference = mStorage.child("photos").child(arrayList.get(y));
try {
final File localFile = File.createTempFile("images", "jpg");
final int finalI = i;
StorageTask<FileDownloadTask.TaskSnapshot> taskSnapshotStorageTask = storageReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Bitmap bitmap = BitmapFactory.decodeFile(localFile.getAbsolutePath());
Resources res = getResources();
final RoundedBitmapDrawable dr =
RoundedBitmapDrawableFactory.create(res, Bitmap.createScaledBitmap(bitmap, 150, 150, false));
dr.setCornerRadius(500);
DatabaseReference myRef2 = database.getReference("chats").child(arrayList.get(finalI)).child("lastMessage");
myRef2.addValueEventListener(new ValueEventListener() {
@SuppressLint("SetTextI18n")
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String message = dataSnapshot.getValue(String.class);
Log.d("TAG", "Value is: " + message);
PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(getActivity());
Preference preference = new Preference(getActivity());
preference.setTitle(message);
preference.setKey("Hej celkom v pohode");
preference.setIcon(dr);
preference.setSelectable(true);
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(getActivity(), ScrollingActivity.class);
startActivity(intent);
return false;
}
});
screen.addPreference(preference);
setPreferenceScreen(screen);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("TAG", "Failed to read value.", error.toException());
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
}
});
} catch (IOException e ) {
}
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("TAG", "Failed to read value.", error.toException());
}
});
代码几乎正在做它应该做的事情。问题是它始终只添加一个Prefence,因为screen.addPreference(preference);和setPreferenceScreen(屏幕);方法在onDataChange中。有没有办法在下载文件后如何使用方法? 而且我认为这段代码很乱,必须以简单的方式下载所有数据。如果你能帮助我,你会非常感激。感谢。