Since the data acquisition process of Firebase is an asynchronous process, when displaying the image and database data in a specific order like the Twitter timeline, if you display it without thinking, the image will shift, so the data will be recursively displayed. The recommended way is to get it.
java.Timeline.java
public class Timeline extends AppCompatActivity{
//List to put the fetched database data
private ArrayList<String> getDate = new ArrayList<>();
//Get image reference
private ArrayList<StorageReference> spaceRef = new ArrayList<>();
//Create DatabaseReference object
private DatabaseReference mDatabase;
//FireBase storage
private FirebaseStorage storage = FirebaseStorage.getInstance();
//storage
private StorageReference storageRef = storage.getReference();
//Count variable
private int Count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Get reference to Database
mDatabase = FirebaseDatabase.getInstance().getReference();
//FireBase Events
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//Data you want to get
Object getObject = postSnapshot.child(/*Data you want to get*/).getValue();
//Null check of acquired data
if(getObject != null) {
//Store in list
getDate.add(getObject .toString());
//Get image reference
spaceRef.add(storageRef.child(/*Reference image*/));
//Processing of the image you want to go to
getPhoto();
}
}
}
//Image acquisition
private void getPhoto() {
int Listsize = spaceRef.size();
//Avoid looking off the list
if(Count < Listsize) {
final long ONE_MEGABYTE = 1024 * 1024;
//Storage event
spaceRef.get(Count).getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
//Thumbnail image acquisition
Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
/*Put the process to add to the list view*/
Count++;
getPhoto();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
}
});
}
}
The original list view has already been created, and I wrote it for those who are stumbling one step further, so it is quite appropriate (I think that the processing actually used is not helpful), but for the time being I managed to do it. .. Also, since it is java, I personally think that it is better to check null. My friend published it on Github, so I'll put a link on it. StutdentTimeline.java and TeacherStudent.java actually use this method. https://github.com/wasabimochi/ivycon When I feel like it, I'll write it neatly and post it (I'm not saying that). Please comment if you have any questions. see you.
Recommended Posts