-3

I'm trying to get specific indexed value by key using arraylist.get(2); but failed, also is throws an exception.

"IndexOutOfBoundsException: Invalid index 2, size is 1"

Where as my arraylist is of size 3 (0-2).

Here's my method code below:

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {

    //add keys to arraylist
    String key=dataSnapshot.getKey();
    arraylist.add(key);

    //it displays found indexes as 0, 1, 2
    int index= arraylist.indexOf(key);
    Log.i("indexs", String.valueOf(index));

    //it displays exception, but when I replace "2" with "0" then, 
    //it prints one value four times which lie on index "1"
    String specificIndexValue = arraylist.get(2);
    Log.i("IndexValues", specificIndexValue);

}
Irfan Akram
  • 29
  • 1
  • 1
  • 11

1 Answers1

1

The first time onChildAdded is called, there will be an exception because arraylist will only have 1 item, and you are accessing index 2 that requires 3 or more items in the list.

String specificIndexValue = arraylist.get(2);

We will need more info to solve your question it prints one value four times which lie on index "1".

It seems you might want to store in an ordered map instead of an array, with the key being the snapshot key and the value being the snapshot.

Try this to debug...

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {

    //add keys to arraylist
    String key=dataSnapshot.getKey();
    arraylist.add(key);

    if (arraylist.size() == 3){
       Log.i("Key: " arraylist.get(2), "Index: 2");
    }
}
Steven Spungin
  • 27,002
  • 5
  • 88
  • 78
  • we dont know how the size of `arraylist` is increasing – The Scientific Method Sep 09 '18 at 19:51
  • Hmm. Method called `onChildAdded` and adding child's id to array list. I would wager that this is the only place... Plus OP has not provided any other context so perhaps it is a safe assumption. – Steven Spungin Sep 09 '18 at 19:55
  • @StevenSpungin - Its running and prints value under indexes like `I/IndexValues-0: Managel life: Bad` `I/IndexValues-1: avarge` `I/IndexValues-2: children life: Good` it seems good. But how to get desire index value from them? – Irfan Akram Sep 09 '18 at 20:09
  • So your 'index' is really the order they were added, yes. If so, to get the index of a snapshot: `arraylist.indexOf(dataSnapshot.getKey())`. This assumes your keys have an appropriate `equals()` implementation... The index is the `i` value of the loop, yes? – Steven Spungin Sep 09 '18 at 20:14
  • @StevenSpungin - It prints only the last indexed number (not value), but still I want to get **String values** (not index number) under the desire indexes. – Irfan Akram Sep 09 '18 at 20:21
  • Array does not use string values as indexes. You can do `"" + i' to get a String from index or String.valueOf(i). I will update solution with loop... – Steven Spungin Sep 09 '18 at 20:25
  • @StevenSpungin - I know you are on right going and now I'm little farther apart to get my problem solved. But still I didn't get solution for my problem. A little request is please update code to get **only and only 2nd indexed string value** from your solution. It would be great help!! – Irfan Akram Sep 09 '18 at 20:44
  • @IrfanAkram Do you want 2nd item or item at index 2 (0 based)? – Steven Spungin Sep 09 '18 at 20:51
  • @StevenSpungin - Thanks!! it worked. great help ever. – Irfan Akram Sep 09 '18 at 21:00