我有一个recycler视图,其中填充了2个arraylist,第一个是卡片视图标题,第二个是它的内容,问题发生在第二个arraylist中,例如,它从firestore接收数据。
下面是如何填充从本地云和字符串接收的数据的arraylist的代码。
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)个位置,这会导致延迟,从而导致2的0跳转(因为1尚未填充)出错。
3条答案
按热度按时间lawou6xi1#
一个快速解决方法是在索引2和索引3中添加数据之前检查arraylist的大小。如果尚未收到来自数据库的数据,则可以:
等待,然后当数据到达时,在添加2和3之前将其添加到索引1
或
将索引1设置为null,然后添加2和3并继续,当数据到达时,它将替换null值
第二个选项的示例:
ogsagwnx2#
你明白吗
exception
因为你的ArrayList
(valorCard
)大小为1,您正在位置2添加项目可以通过如下方式创建数组列表时设置数组列表大小来更正此问题:
但未设置的值将
null
防止null
创建时可以使用此代码valorCard
:qnakjoqk3#
这个问题的原因是一个方法“add”来自arraylist,这个方法覆盖最早的position值并“put”到下一个地址,解决方法是使用set方法,所以,然后是“valorcard.add”我覆盖到valorcard.set。