简体   繁体   中英

ArrayList returns last value I added

I have 2 ArrayLists.

ArrayList<String> brands = new ArrayList<>()
ArrayList<ArrayList<String>> finalBrands = new ArrayList<>();

Code sample:

reference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                Keylist.add(dataSnapshot.getKey());
                Valuelist.add(dataSnapshot.getValue().toString());
            }
            if (Keylist.size()==snapshot.getChildrenCount()){
                for (int i =0; i<Keylist.size();i++){
                    reference1.child(Valuelist.get(i)).child(Keylist.get(i)).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshot) {
                            for (DataSnapshot dataSnapshot: snapshot.getChildren()){
                                if (dataSnapshot.hasChildren()){
                                    brands.add(dataSnapshot.getKey());
                                    conditions.add(dataSnapshot.child("condition").getValue().toString());
                                }
                                if (brands.size()==snapshot.getChildrenCount()-5){
                                    finalBrands.add(brands);
                                    finalConditions.add(conditions);
                                    Log.d("glavniy", finalBrands.toString());
                                    brands.clear();
                                    conditions.clear();
                                }
                            }
                        }


                        @Override
                        public void onCancelled(@NonNull DatabaseError error) {

                        }
                    });
                }
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

It has to return [[burgerking, gyros, kfc], [gyros, kfc], [gyros]], but it autofills himself by last value I added.

Log:

2021-04-11 12:41:49.272 27641-27641/com.example.main D/glavniy: [[burgerking, gyros, kfc]]
2021-04-11 12:41:49.299 27641-27641/com.example.main D/glavniy: [[gyros, kfc], [gyros, kfc]]
2021-04-11 12:41:49.318 27641-27641/com.example.main D/glavniy: [[gyros], [gyros], [gyros]]

PS I initialized both arraylists in the class(not in method), so it cant be because of that. PS ArrayLists are private and non static.

Finally I found a solution. Thanks @HenryTwist

I just needed to replace brands.clear() and conds.clear() with brands = new ArrayList<>() and conds = new ArrayList<>()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM