我的代码需要一些帮助。我无法解决问题。我正在使用日志进行调试。 onDataChange()方法Log.i("SELFIES", image_name);
内的其中一个,另外两个Log.i("SELFIEFRAGMENT", String.valueOf(mPhotoIds.size()));
不在此方法之内。
问题是,它最初没有进入onDataChange方法,而是经过它,打印数组的大小,返回视图。之后,它返回并进入方法并打印获取的数据,然后完成操作(我的目标是在其他方法中使用mPhotoIds数组,但会出现此问题)。
调试器控制台输出:
I/SELFIEFRAGMENT: Array size is : 0
I/SELFIEFRAGMENT: Array size is : 0
I/Choreographer: Skipped 998 frames! The application may be doing too much work on its main thread.
I/SELFIES: asdasd_giris_25.54.47_29.Mayıs.2019
I/SELFIES: fdsafdas_giris_25.55.4_29.Mayıs.2019
I/SELFIES: sdf_cikis_8.44.32_30.Mayıs.2019
我的代码:
package com.newstrange.worklog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import java.util.ArrayList;
public class ListSelfiesFragment extends Fragment {
private FirebaseStorage mStorageIns;
private FirebaseDatabase mDatabaseIns;
private RecyclerView mRecyclerView;
private GridLayoutManager mLayoutManager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mDatabaseIns = FirebaseDatabase.getInstance(); // DATABASE
mStorageIns = FirebaseStorage.getInstance(); // STORAGE
View view = inflater.inflate(R.layout.fragment_list_selfies, container, false);
mRecyclerView = view.findViewById(R.id.selfies_recyclerview);
//initialize lists
final ArrayList<String> mPhotoIds = new ArrayList<>();
final ArrayList<Bitmap> mPhotoArrayList = new ArrayList<>();
//get names
DatabaseReference usersPhotoRef = mDatabaseIns.getReference().child("storeIDs");
usersPhotoRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot uniqueKeySnapshot : dataSnapshot.getChildren()) {
String image_name = uniqueKeySnapshot.getValue(String.class);
Log.i("SELFIES", image_name); // PRINTING when everything is finished
mPhotoIds.add(image_name);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.i("SELFIEFRAGMENT", databaseError.getMessage());
}
});
Log.i("SELFIEFRAGMENT", "Array size is : " +mPhotoIds.size()); // PRINTS 0
if (!mPhotoIds.isEmpty()) {
final long ONE_MEGABYTE = 1024 * 1024;
for (int i = 0; i < mPhotoIds.size(); i++) {
StorageReference photoRef = mStorageIns.getReference().child("images/" + mPhotoIds.get(i) + ".png");
// final File localFile = File.createTempFile(mPhotoIds.get(i), "png"); // içine yüklenecek değişken
photoRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
mPhotoArrayList.add(bitmap);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("SELFIEFRAGMENT", e.getMessage());
}
});
}
}
SelfiesAdapter selfiesAdapter = new SelfiesAdapter(getActivity(), mPhotoArrayList, mPhotoIds);
mRecyclerView.setAdapter(selfiesAdapter);
mLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
Log.i("SELFIEFRAGMENT", "Array size is : " + mPhotoIds.size()); // PRINTS 0
// PRINTS 0
return view;
}
}
答案 0 :(得分:1)
您必须在onDataChange
的for循环之后立即调用Firebase存储功能。
//initialize lists
final ArrayList<String> mPhotoIds = new ArrayList<>();
final ArrayList<Bitmap> mPhotoArrayList = new ArrayList<>();
//get names
DatabaseReference usersPhotoRef = mDatabaseIns.getReference().child("storeIDs");
usersPhotoRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot uniqueKeySnapshot : dataSnapshot.getChildren()) {
String image_name = uniqueKeySnapshot.getValue(String.class);
Log.i("SELFIES", image_name); // PRINTING when everything is finished
mPhotoIds.add(image_name);
}
if (!mPhotoIds.isEmpty()) {
final long ONE_MEGABYTE = 1024 * 1024;
for (int i = 0; i < mPhotoIds.size(); i++) {
StorageReference photoRef = mStorageIns.getReference().child("images/" + mPhotoIds.get(i) + ".png");
// final File localFile = File.createTempFile(mPhotoIds.get(i), "png"); // içine yüklenecek değişken
photoRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
mPhotoArrayList.add(bitmap);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("SELFIEFRAGMENT", e.getMessage());
}
});
}
}
SelfiesAdapter selfiesAdapter = new SelfiesAdapter(getActivity(), mPhotoArrayList, mPhotoIds);
mRecyclerView.setAdapter(selfiesAdapter);
mLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
Log.i("SELFIEFRAGMENT", "Array size is : " + mPhotoIds.size()); // PRINTS 0
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.i("SELFIEFRAGMENT", databaseError.getMessage());
}
});