我正在从json数据中解析图像列表,我需要将图像添加到Picasso然后根据用户点击的位置显示它但我有一个错误:
FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.moayed.appshow.Adapters.ImageListAdapter.onBindViewHolder(ImageListAdapter.java:41)
at com.example.moayed.appshow.Adapters.ImageListAdapter.onBindViewHolder(ImageListAdapter.java:18)
班级
public class ImageListFragment extends Fragment {
List<AppShowModule> appShowModules;
List<AppShowModule> imagesModule;
RecyclerView AppRecyclerView;
RecyclerView.Adapter imageRecyclerViewadapter;
List<String> imageUrls;
String feedKey = "feed";
String entryKey = "entry";
String imageKey = "im:image";
String labelKey = "label";
String jsonUrl = "https://itunes.apple.com/jo/rss/topfreeapplications/limit=50/json";
RequestQueue requestQueue;
private RecyclerView.LayoutManager mLayoutManager;
public ImageListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_image_list, container, false);
}
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AppRecyclerView = (RecyclerView) getView().findViewById(R.id.imageRecyclerView);
imagesModule = new ArrayList<>();
appShowModules = new ArrayList<>();
imageUrls = new ArrayList<>();
JsonAppShowData();
}
public void JsonAppShowData() {
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( jsonUrl, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) { try {
JSONArray jsonArray = response.getJSONObject(feedKey).getJSONArray( entryKey );
AppShowModule appShowModule = new AppShowModule();
int x = appShowModule.getId();
for (int i = 0 ; i<jsonArray.length() ;i++)
{
JSONArray imageArray = response.getJSONObject(feedKey).getJSONArray(entryKey).getJSONObject(x).getJSONArray(imageKey);
for (int j = 0; j < imageArray.length(); j++) {
String image = imageArray.getJSONObject(x).getString(labelKey).toString();
imageUrls.add(image);
appShowModule.setAllimage(imageUrls);
appShowModules.add(appShowModule);
}}
imageRecyclerViewadapter = new ImageListAdapter(appShowModules, getContext(), imageUrls);
AppRecyclerView.setAdapter(imageRecyclerViewadapter);
} catch (JSONException e) {
e.printStackTrace();
}}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e( "LOG", error.toString() );
}
} );
requestQueue = Volley.newRequestQueue( getContext() );
requestQueue.add(jsonObjectRequest);
mLayoutManager = new GridLayoutManager( getContext().getApplicationContext(),3);
AppRecyclerView.setLayoutManager(mLayoutManager); }}
适配器
public class ImageListAdapter extends RecyclerView.Adapter<ImageListAdapter.ViewHolder> {
List<AppShowModule> appShowModules;
List<String> imageUrl;
AppShowModule appShowModule;
Context context;
public ImageListAdapter(List<AppShowModule> appShowModules, Context context ,List<String>imageUrls
){
super();
this.imageUrl =imageUrls;
this.appShowModules = appShowModules;
this.context = context;}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from( parent.getContext() ).inflate( R.layout.imagelayout, parent,false );
ViewHolder viewHolder = new ViewHolder( v );
return viewHolder;}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
AppShowModule appShowModule = new AppShowModule();
Picasso.with(context).load(appShowModule.getAllimage().get(position)).into(holde r.appImage);
}
public int getItemCount() {
return imageUrl.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public ImageView appImage;
public ViewHolder(View itemView) {
super(itemView);
appImage = (ImageView) itemView.findViewById( R.id.appImage);
}}}
答案 0 :(得分:0)
问题出在你的onBinderView方法中,你通过创建新实例来初始化AppShowModule
,然后在尝试访问已经声明为new且已经为空的数据之后。
public void onBindViewHolder(ViewHolder holder, int position) {
AppShowModule appShowModule = new AppShowModule();
Picasso.with(context).load(appShowModule.getAllimage().get(position)).into(holde r.appImage);
}
更改上面的代码如下:
public void onBindViewHolder(ViewHolder holder, int position) {
AppShowModule appShowModule = appShowModules.get(position);
//Get the position from the list of your `appShowModules` and then try to access image from `AppShowModule`.
Picasso.with(context).load(appShowModule.getAllimage().get(position)).into(holde r.appImage);
}
希望这会对你有所帮助。
如果有任何疑问,请告诉我。