luke cross
luke cross

Reputation: 331

ArrayList IndexOfBound exceptions when to get data from firestore database, why?

i have a Recycler View which is filled with 2 arraylist, the first is the card view title, and the second its content, the problem happens in the second arraylist, which receives data from the firestore, for example.

Below is the code for how I fill the arraylist of data I receive from the local cloud and strings.

        valorCard.add(0, "R$: 16.402,98"); // must be added on first position

        DocumentReference dbRef = db.collection("Usuarios").document(firebaseAuth.getCurrentUser().getUid().toString());
        dbRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>(){
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                valorCard.add(1, documentSnapshot.get("quantBois").toString()); // must be added on second position
            }
        });
        valorCard.add(2,"25.000,00$"); // must be added on thrid position
        valorCard.add(3,"25/05/2020"); // must be added on fourth position

When the code is runned, this is returned:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.agrolucros2019, PID: 29318
    java.lang.IndexOutOfBoundsException: Index: 2, Size: 1
        at java.util.ArrayList.add(ArrayList.java:483)
        at com.example.agrolucros2019.fragmentos.home.HomeFragment.onCreateView(HomeFragment.java:65)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
        at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2663)
        at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
        at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2624)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:904)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
        at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1395)
        at android.app.Activity.performStart(Activity.java:7361)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3150)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1960)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7094)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)

I believe this is due to the fact that a request for data in the cloud takes some time to receive the data, and as I need to insert the data exactly in the second (1) position, this causes a delay that causes the 0 jump for 2 (since 1 has not yet been filled in) go wrong.

Upvotes: 0

Views: 190

Answers (3)

luke cross
luke cross

Reputation: 331

This cause of my problem is a method "add" from ArrayList, this method overwrite the oldest value of position and "put" to the next address, the solution was to use the Set method, so, then were "valorCard.add" i'm overwrite to valorCard.set.

Upvotes: 0

Amani
Amani

Reputation: 3977

you get this exception because your ArrayList (valorCard) size is 1 and you are adding item at position 2

you can correct this by setting array list size when creating it like this:

ArrayList<String> valorCard = ArrayList<>(4);

but values that is not set will be null to prevent null you can use this code when creating valorCard:

ArrayList<String> valorCard = ArrayList<>();
valorCard.addAll( { "", "", "", "" } );// four strings for size of 4

Upvotes: 1

Zen Monkey
Zen Monkey

Reputation: 101

A quick fix would be to check the size of the ArrayList before you add the data in index 2 and 3. If the data from the database hasn't been received, you can:

  1. Wait, and then when the data arrives add it to index 1 before adding 2 and 3

OR

  1. Set index 1 to null and then add 2 and 3 and move on, and when the data arrives it will replace the null value

An example of the 2nd option:

dbRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>(){
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                valorCard.add(1, documentSnapshot.get("quantBois").toString()); // must be added on second position
            }
        });
        if (valorCard.size() == 1){ // Check here
            valorCard.add(null);
        }
        valorCard.add(2,"25.000,00$"); // must be added on thrid position
        valorCard.add(3,"25/05/2020"); // must be added on fourth position

Upvotes: 1

Related Questions