我有一个用2个arraylist填充的Recycler View,第一个是卡片视图标题,第二个是其内容,问题发生在第二个arraylist中,例如,它从Firestore接收数据。
以下是有关如何填充从本地云和字符串接收的数据的数组列表的代码。
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
运行代码后,将返回以下内容:
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)
我认为这是由于以下事实:云中对数据的请求需要一些时间来接收数据,并且由于我需要将数据准确地插入第二(1)位置,因此会导致延迟,导致0跳2(因为尚未填写1)出错。
最佳答案
一种快速修复是在将数据添加到索引2和3中之前检查ArrayList的大小。如果尚未收到来自数据库的数据,则可以:
等待,然后在数据到达时将其添加到索引1,然后再添加2和3
要么
将索引1设置为null,然后添加2和3并继续,当数据到达时它将替换为null值
第二个选项的示例:
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