- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从网络服务获取数据并根据标题将其存储到列表中。例如,如果响应包含音乐,它应该添加到音乐列表等。当数据仅来自音乐、电影等单一类型时,没有问题。只要有两种或多种类型可用,并且我正在尝试,我就会遇到异常将它们设置在适配器中。我在谷歌上搜索了解决方案,但没有发现任何有用的东西来解决我的问题。
这是我的 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/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!