0

This my code. onooltuud is an ArrayList. I get datas Firebase then add the onooltuud arrayList. But arrayList size 0. Caution incoming data from Firebase. Help me.

 public class OnooltActivity2 extends AppCompatActivity implements View.OnClickListener {
ArrayList<String> onooltuud = new ArrayList<String>();`

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.onoolt_activity_2);



    save = (Button) findViewById(R.id.save);

    database = FirebaseDatabase.getInstance();
    ref = database.getReference("db").child("davaa").child(String.valueOf(davaaniiNo) + "-iinDavaa");
    Query query = ref.orderByChild("onooltDugaar");

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            int temp = (int) dataSnapshot.getChildrenCount();
            Log.i("count", String.valueOf(temp));
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                Log.i("snapshot", snapshot.getValue().toString());
                Onoolt onoolt = snapshot.getValue(Onoolt.class);
                onoolts.add(onoolt.getOnooltDugaar());
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(OnooltActivity2.this, "Датаг уншиж чадсангүй: " + databaseError.getCode(), Toast.LENGTH_SHORT).show();
        }

    });

    Log.i("testSize:", String.valueOf(onoolts.size()));

this my code onoolts add not working

5
  • Where are you adding to onooltuud? Commented May 27, 2018 at 16:00
  • 1
    You array is called onooltuud, instead or onoolts.add(onoolt.getOnooltDugaar()); put onooltuud.add(onoolt.getOnooltDugaar()); Commented May 27, 2018 at 16:02
  • You just log in the error place Commented May 27, 2018 at 17:01
  • @LieForBananas :: Maybe you should add that solution tip as an Answer! I don't thing the OP is able to understand your suggest without a more structured response. Commented May 27, 2018 at 17:31
  • @Barns you are right, I'll add it as an answer with more details :) Commented May 27, 2018 at 17:37

1 Answer 1

1

It says that the size() of your Arraylist object onooltuud is zero (= 0) because you never add anything to it. You declared your ArrayList like that:

ArrayList<String> onooltuud = new ArrayList<String>();

And when you add elements you use a different name:

onoolts.add(onoolt.getOnooltDugaar());

The names do not correspond: onooltuud vs onoolts.

Delete this line:

onoolts.add(onoolt.getOnooltDugaar()); 

and put this line:

onooltuud.add(onoolt.getOnooltDugaar());
Sign up to request clarification or add additional context in comments.

1 Comment

@Barns Let's hope so! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.