RecyclerView 在向下滚动时崩溃(IndexOutOfBoundsException:索引:2,大小:2)
RecyclerView Crashes on Scrolldown ( IndexOutOfBoundsException: Index: 2, Size: 2 )
iam 从 json 解析数据并将其传递给包含两个 Arraylist 的 ArrayList,然后将它们传递给 RecyclerView,RecyclerView 显示数据,但 1 项占用整个页面,当我向下滚动时我可以看到第二个项目然后崩溃
我通过 parent 或回收商的观点尝试了很多人的建议
layout_heigt="wrap_content" 连页面都打不开
这是回收商的视图 xml(activity_resultsdwldview) 和 row.xml 其中包含每一行的卡片视图设计
真的很抱歉,如果这没有组织好或不清楚,我时间真的很短,而且是初级 android 开发人员,经验不多
activity_resultsdwldview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".Resultsdwldview">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyvlerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/nameArray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp" />
<TextView
android:id="@+id/URLName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
适配器Class
package com.example.imc;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
List<ArrayList<String>> Tests;
ArrayList<String> urlArray;
ArrayList<String> nameArray;
public MainAdapter(List<ArrayList<String>> tests, ArrayList<String> urlArray, ArrayList<String> nameArray) {
Tests = tests;
this.urlArray = urlArray;
this.nameArray = nameArray;
}
@NonNull
@Override
public MainAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row,viewGroup,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MainAdapter.ViewHolder holder, int i) {
holder.URLName.setText( urlArray.get(i));
holder.mFullname.setText( nameArray.get(i));
}
@Override
public int getItemCount() {
return Tests.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView URLName;
public TextView mFullname;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mFullname=itemView.findViewById(R.id.nameArray);
URLName=itemView.findViewById(R.id.URLName);
}
}
}
Here's the Error log
E/RecyclerView: No adapter attached; skipping layout
D/EGL_emulation: eglMakeCurrent: 0xa2cede60: ver 2 0 (tinfo 0xa2cefaf0)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.imc, PID: 16616
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.get(ArrayList.java:411)
at com.example.imc.MainAdapter.onBindViewHolder(MainAdapter.java:40)
at com.example.imc.MainAdapter.onBindViewHolder(MainAdapter.java:13)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:286)
at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:343)
at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:359)
at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:366)
at android.support.v7.widget.GapWorker.run(GapWorker.java:397)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
enter code here
您的适配器大小由以下人员设置:
@Override
public int getItemCount() {
return Tests.size();
}
你试图从以下位置获取物品:
@Override
public void onBindViewHolder(@NonNull MainAdapter.ViewHolder holder, int i) {
holder.URLName.setText( urlArray.get(i));
holder.mFullname.setText( nameArray.get(i));
}
您需要将适配器大小设置为您要检索的大小:
@Override
public int getItemCount() {
urlArray.size()
}
或
@Override
public int getItemCount() {
return nameArray.sizes()
}
我建议使用单个数组,或者可能使用映射,以避免类似问题。
iam 从 json 解析数据并将其传递给包含两个 Arraylist 的 ArrayList,然后将它们传递给 RecyclerView,RecyclerView 显示数据,但 1 项占用整个页面,当我向下滚动时我可以看到第二个项目然后崩溃
我通过 parent 或回收商的观点尝试了很多人的建议 layout_heigt="wrap_content" 连页面都打不开 这是回收商的视图 xml(activity_resultsdwldview) 和 row.xml 其中包含每一行的卡片视图设计
真的很抱歉,如果这没有组织好或不清楚,我时间真的很短,而且是初级 android 开发人员,经验不多
activity_resultsdwldview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".Resultsdwldview">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyvlerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/nameArray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp" />
<TextView
android:id="@+id/URLName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
适配器Class
package com.example.imc;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
List<ArrayList<String>> Tests;
ArrayList<String> urlArray;
ArrayList<String> nameArray;
public MainAdapter(List<ArrayList<String>> tests, ArrayList<String> urlArray, ArrayList<String> nameArray) {
Tests = tests;
this.urlArray = urlArray;
this.nameArray = nameArray;
}
@NonNull
@Override
public MainAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row,viewGroup,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MainAdapter.ViewHolder holder, int i) {
holder.URLName.setText( urlArray.get(i));
holder.mFullname.setText( nameArray.get(i));
}
@Override
public int getItemCount() {
return Tests.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView URLName;
public TextView mFullname;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mFullname=itemView.findViewById(R.id.nameArray);
URLName=itemView.findViewById(R.id.URLName);
}
}
}
Here's the Error log
E/RecyclerView: No adapter attached; skipping layout
D/EGL_emulation: eglMakeCurrent: 0xa2cede60: ver 2 0 (tinfo 0xa2cefaf0)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.imc, PID: 16616
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.get(ArrayList.java:411)
at com.example.imc.MainAdapter.onBindViewHolder(MainAdapter.java:40)
at com.example.imc.MainAdapter.onBindViewHolder(MainAdapter.java:13)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:286)
at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:343)
at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:359)
at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:366)
at android.support.v7.widget.GapWorker.run(GapWorker.java:397)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
enter code here
您的适配器大小由以下人员设置:
@Override
public int getItemCount() {
return Tests.size();
}
你试图从以下位置获取物品:
@Override
public void onBindViewHolder(@NonNull MainAdapter.ViewHolder holder, int i) {
holder.URLName.setText( urlArray.get(i));
holder.mFullname.setText( nameArray.get(i));
}
您需要将适配器大小设置为您要检索的大小:
@Override
public int getItemCount() {
urlArray.size()
}
或
@Override
public int getItemCount() {
return nameArray.sizes()
}
我建议使用单个数组,或者可能使用映射,以避免类似问题。