gpt4 book ai didi

java - 在android中的recyclerview的adapterview中组合两个列表时获取IndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-02 10:08:55 25 4
gpt4 key购买 nike

我正在从网络服务获取数据并根据标题将其存储到列表中。例如,如果响应包含音乐,它应该添加到音乐列表等。当数据仅来自音乐、电影等单一类型时,没有问题。只要有两种或多种类型可用,并且我正在尝试,我就会遇到异常将它们设置在适配器中。我在谷歌上搜索了解决方案,但没有发现任何有用的东西来解决我的问题。

这是我的 Recycler View 的 Adapter 类:

    public class HomeSearchAdapter extends RecyclerView.Adapter<HomeSearchAdapter.Holder> {

Context context;

List<Music> listMusic = new ArrayList<>();
List<AllPremiumMovies> moviesPremium = new ArrayList<>();
List<VideoPojo> listVideos = new ArrayList<>();
List<Movie> movieList = new ArrayList<>();
int items;

private DisplayImageOptions options;
public static Clickitem clickitem;


@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.homesearchadapter, parent, false);
return new Holder(view);
}

public HomeSearchAdapter(Context context, List<Music> listMusic, List<AllPremiumMovies> moviesPremium, List<VideoPojo> listVideos, List<Movie> movieList) {
this.context = context;
this.listMusic = listMusic;
this.moviesPremium = moviesPremium;
this.listVideos = listVideos;
this.movieList = movieList;
}

@Override
public void onBindViewHolder(@NonNull final Holder holder, final int position) {
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.moremovierealicon)
.showImageForEmptyUri(R.drawable.moremovierealicon)
.showImageOnFail(R.drawable.moremovierealicon)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();


// getMusicData();//getting music data
// getPreData();
// getVideos();
if (movieList != null && moviesPremium != null) {
for (int i = 0; i < movieList.size(); i++) {
holder.name.setText(movieList.get(position).getMovieName());
holder.genre.setText(movieList.get(position).getNewGenere());
holder.woods.setText(movieList.get(position).getNewWoods());
holder.artistName.setVisibility(View.GONE);
holder.rating.setText(movieList.get(position).getRating());
holder.type.setText("Movie");

ImageLoader.getInstance()
.displayImage(movieList.get(position).getThumbnail(), holder.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {

}

@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {

}
});
}
for (int i = 0; i < moviesPremium.size(); i++) {
holder.name.setText(moviesPremium.get(position).getMovieName());
holder.genre.setText(moviesPremium.get(position).getNewGenre());
holder.woods.setText(moviesPremium.get(position).getNewMovieWoods());
holder.artistName.setVisibility(View.GONE);
holder.rating.setText(moviesPremium.get(position).getRating());
holder.type.setText("Premium Movie");

ImageLoader.getInstance()
.displayImage(moviesPremium.get(position).getThumbnail(), holder.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {

}

@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {

}
});
}

} else if (movieList != null) {
holder.name.setText(movieList.get(position).getMovieName());
holder.genre.setText(movieList.get(position).getNewGenere());
holder.woods.setText(movieList.get(position).getNewWoods());
holder.artistName.setVisibility(View.GONE);
holder.rating.setText(movieList.get(position).getRating());
holder.type.setText("Movie");

ImageLoader.getInstance()
.displayImage(movieList.get(position).getThumbnail(), holder.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {

}

@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {

}
});

} else if (moviesPremium != null) {
holder.name.setText(moviesPremium.get(position).getMovieName());
holder.genre.setText(moviesPremium.get(position).getNewGenre());
holder.woods.setText(moviesPremium.get(position).getNewMovieWoods());
holder.artistName.setVisibility(View.GONE);
holder.rating.setText(moviesPremium.get(position).getRating());
holder.type.setText("Premium Movie");

ImageLoader.getInstance()
.displayImage(moviesPremium.get(position).getThumbnail(), holder.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {

}

@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {

}
});

} else if (listMusic != null) {
holder.name.setText(listMusic.get(position).getSongName());
holder.genre.setText(listMusic.get(position).getCatName());
holder.artistName.setText(listMusic.get(position).getArtist());
holder.rating.setText(listMusic.get(position).getRating());
holder.type.setText("Music");

ImageLoader.getInstance()
.displayImage(listMusic.get(position).getThumbnailUrl(), holder.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {

}

@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {

}
});
} else if (listVideos != null) {
holder.genre.setText(listVideos.get(position).getNewVideoGenre());
holder.woods.setText(listVideos.get(position).getNewVideoWoods());
holder.rating.setVisibility(View.GONE);
holder.artistName.setVisibility(View.GONE);
holder.type.setText("Videos");

ImageLoader.getInstance()
.displayImage(listVideos.get(position).getVideoThumb(), holder.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {

}

@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {

}
});
}


holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, holder.type.getText(), Toast.LENGTH_LONG).show();
String typeOtype = String.valueOf(holder.type.getText());
if (typeOtype.equals("Movies")) {
Toast.makeText(context, "will go to movie desc", Toast.LENGTH_LONG).show();
} else if (typeOtype.contains("Music")) {
Intent intent = new Intent(context, MusicPlayerActivity.class);
intent.putExtra("pos", position);
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST", (Serializable) listMusic);
intent.putExtra("BUNDLE", args);
context.startActivity(intent);

} else if (typeOtype.equals("Premium Movie")) {
Intent intent = new Intent(context, PremiumDescriptionActivity.class);
intent.putExtra("movieId", position);
Bundle args = new Bundle();
args.putSerializable("PremiumMovieArrayList", (Serializable) moviesPremium);
intent.putExtra("BUNDLE", args);
context.startActivity(intent);
} else if (typeOtype.contains("Videos")) {
Toast.makeText(context, "will go to video desc", Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, MovieDescriptionActivity.class);
intent.putExtra("videoId", position);
Bundle args = new Bundle();
args.putSerializable("VideoArrayList", (Serializable) listVideos);
intent.putExtra("BUNDLE", args);
context.startActivity(intent);
} else if (typeOtype.contains("Movies")) {
Intent intent = new Intent(context, MovieDescriptionActivity.class);
intent.putExtra("movieId", position);
Bundle args = new Bundle();
args.putSerializable("MovieArrayList", (Serializable) movieList);
intent.putExtra("BUNDLE", args);
context.startActivity(intent);
} else {
Log.v("Exceptional Case in", "HomeSearchAdapter");
}
}
});
}

@Override
public int getItemCount() {


if (movieList != null && moviesPremium != null) {

items = (movieList.size());
Log.v("size total", String.valueOf(items));
Log.v("size total", String.valueOf(items));


} else if (movieList != null) {
items = movieList.size();
} else if (moviesPremium != null) {
items = moviesPremium.size();
} else if (listMusic != null) {
items = listMusic.size();
} else if (listVideos != null) {
items = listVideos.size();
} else {
Log.v("some error", "in home search adapter");
}


return items;
}

class Holder extends RecyclerView.ViewHolder {

ImageView imageView;
TextView name, artistName, type, rating, woods, genre;

ConstraintLayout constraintLayout;


public Holder(final View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.dataImage);
name = itemView.findViewById(R.id.dataName);


type = itemView.findViewById(R.id.dataType);
rating = itemView.findViewById(R.id.dataRating);
constraintLayout = itemView.findViewById(R.id.clickmelayout);
artistName = itemView.findViewById(R.id.dataArtist);
woods = itemView.findViewById(R.id.dataWoods);
genre = itemView.findViewById(R.id.dataGenre);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

clickitem.onitemsclicklistener(getAdapterPosition(), view);

}
});

}

}

public void setItemClick(Clickitem click) {
HomeSearchAdapter.clickitem = click;
}//setClick

public interface Clickitem {
void onitemsclicklistener(int position, View view);
}







}

这是我的回收器 View ,我在其中获取数据。这没有任何问题。但是当两个或多个列表不为空时意味着数据在 andapter 中创建异常。

  Call<HomeSearchAllPojo> caller = ApiClient.getInstance().getApiService().getHomeSearchDataWithPojo(queryName);
caller.enqueue(new Callback<HomeSearchAllPojo>() {
@Override
public void onResponse(Call<HomeSearchAllPojo> call, Response<HomeSearchAllPojo> response) {
if (response.body() != null) {
movieList = response.body().getMovies();
}
if (response.body() != null) {
premiumList = response.body().getPremium();
}
if (response.body() != null) {
musicList = response.body().getMusic();
}
if (response.body() != null) {
videoList = response.body().getVideos();
}
movieAdapter = new HomeSearchAdapter(getContext(), musicList, premiumList, videoList, movieList);
recyclerView.setAdapter(movieAdapter);
}

@Override
public void onFailure(Call<HomeSearchAllPojo> call, Throwable t) {

}
});

错误:

    java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.get(ArrayList.java:437)
at luck.materialdesign.tabsnavigator.adapters.HomeSearchAdapter.onBindViewHolder(HomeSearchAdapter.java:97)
at luck.materialdesign.tabsnavigator.adapters.HomeSearchAdapter.onBindViewHolder(HomeSearchAdapter.java:46)
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.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
at android.view.View.measure(View.java:22071)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22071)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:724)
at android.view.View.measure(View.java:22071)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2422)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1504)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1761)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
2019-03-13 13:06:49.265 7078-7078/luck.materialdesign.tabsnavigator E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

最佳答案

如果数组或集合的大小为 1,则最大索引为 0。

Java 是从 0 开始的,因此,您获取的第一个元素是:myList.get(0);myArray[0];

您的错误消息告诉您,您正在尝试获取仅包含一个元素的 ArrayList 的第二个元素。

编辑:在您的代码中,问题可能出在这里:

for (int i = 0; i < movieList.size(); i++) {
holder.name.setText(movieList.get(position).getMovieName());

在那里,我们知道 i 将是您列表的有效索引,但这并不意味着位置是。

关于java - 在android中的recyclerview的adapterview中组合两个列表时获取IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55137050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com