0

I don't know why the value of ArrayList 'lalala' doesn't change outside of the onDataChange function in the onCreateView..

I expected 2 in the line "Log.d("lalala_size", String.valueOf(lalala.size()));" but, it shows 0

I thought the scope of the ArrayList lalala but I can't fix this problem.

I wanted to get the address of images in the firebase realtime database..

please help me

public class Fragment2  extends Fragment implements ImageAdapter.OnListItemSelectedInterface, ImageAdapter.OnListItemLongSelectedInterface{
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
GridView gridView;
static int screenWidth;
private ImageView imageView;
RecyclerView recyclerView;
GridLayoutManager gridLayoutManager;
private GalleryManager mGalleryManager;
public ArrayList<imgFormat> localPhotoList;

private DatabaseReference mDatabaseRef;


private ArrayList<Item> list = new ArrayList<>();
ArrayList<ImageUrl> lalala = new ArrayList<>();
static ArrayList<ImageUrl> imageUrlList = new ArrayList<>();


FirebaseDatabase mFirebaseDatabase;
DatabaseReference mDatabaseReference;

FirebaseStorage storage;
StorageReference storageReference;

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

public Fragment2() {
    // Required empty public constructor
}

// TODO: Rename and change types and number of parameters
public static Fragment2 newInstance(String param1, String param2) {
    Fragment2 fragment = new Fragment2();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //Log.d("dispsize", "calculate the display size");
    //DisplayMetrics metrics = this.getResources().getDisplayMetrics();
    //screenWidth = metrics.widthPixels;
    //Log.d("dispsize", String.valueOf(screenWidth));
    View v = inflater.inflate(R.layout.fragment_2, container, false);

    imageView = (ImageView) v.findViewById(R.id.imageView);
    recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView2);
    gridLayoutManager = new GridLayoutManager(getActivity().getApplicationContext(), 3);
    recyclerView.setLayoutManager(gridLayoutManager);
    
    mDatabaseRef = FirebaseDatabase.getInstance().getReference("name_list");
    mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot postSnapshot: dataSnapshot.getChildren()){
                String key=postSnapshot.getKey();
                FirebasePost get=postSnapshot.getValue(FirebasePost.class);
                String[] info={get.name,get.number,get.img};
                Item result= new Item(info[0],info[1]);

                list.add(result);
                if(info[2]!=null){
                    ImageUrl imageUrl = new ImageUrl();
                    imageUrl.setImageUrl(info[2]);
                    imageUrlList.add(imageUrl);
                    lalala.add(imageUrl);
                    Log.d("imgSaved", String.valueOf(imageUrlList.size()));
                }
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            //Toast.makeText(fragement,"Error", Toast.LENGTH_SHORT).show();
        }
    });

    Log.d("lalala_size", String.valueOf(lalala.size())); // THIS IS THE PROBLEMATIC LINE... i expected 2 but the log shows 0
    for (int i = 0; i < imageUrlList.size(); i++){
        Log.d("images", ">> " + imageUrlList.get(i).getImageUrl());
    }
    Log.d("images", "List count: " + imageUrlList.size());

    // Inflate the layout for this fragment
    return v;
}

Thank you

Youngjae
  • 47
  • 1
  • 8
  • [because it is an async operation](https://stackoverflow.com/questions/57330766/why-does-my-function-that-calls-an-api-return-an-empty-or-null-value) – a_local_nobody Jul 13 '20 at 12:11
  • Then.. how can I change the code to get imageUrlList(or lalala) from the code? – Youngjae Jul 13 '20 at 12:15
  • 1
    onDataChange provides you with the relevant data from the server, as soon as something changes with the data it will be returned there and only there, if you add breakpoints or log statements into that scope you will see that everything works as you expect it to – a_local_nobody Jul 13 '20 at 12:18
  • onDataChange will be called (hence the name) every time the data changes, so you can add any relevant code inside there to update and change your UI and it will be called each time – a_local_nobody Jul 13 '20 at 12:20
  • I added ImageAdapter dataAdapter = new ImageAdapter(getActivity().getApplicationContext(), imageUrlList, localPhotoList, this, this); in to the onDataChange as you said, but I am confused as to what to turn "this" into. – Youngjae Jul 14 '20 at 02:19

0 Answers0