将来自Json的数据解析为毕加索的图像集合 [英] Parsing data from Json as a collection of image to Picasso

查看:40
本文介绍了将来自Json的数据解析为毕加索的图像集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Picasso库将图像集合解析为gridviewrecyclerview,但是我无法使其工作

I tried to parse a collection of image to recyclerview as gridview using Picasso library but i cannot make it work

注意:我没有任何错误,但无法正常工作 这是Json数据链接

Note : i dont have any error but not working Here's Json data a link

我的模块

public class ImagesModule

{
 public ArrayList getListTest() {
    return listTest;
}

public void setListTest(ArrayList listTest) {
    this.listTest = listTest;
}

ArrayList listTest = new ArrayList( );

}

这是适配器`

public class ImageViewAdapter extends RecyclerView.Adapter<ImageViewAdapter.ViewHolder> {
List<ImagesModule> imagesModules;
Context context;

public ImageViewAdapter(List<ImagesModule> imagesModules, Context context){
    super();
    this.imagesModules = imagesModules;
    this.context = context;}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from( parent.getContext() ).inflate( R.layout.imageitem, parent,false );
    ViewHolder viewHolder = new ViewHolder( v );

    return viewHolder;}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final ImagesModule imagesModule = imagesModules.get( position );
    Picasso.with(context).load(String.valueOf(imagesModule.getListTest())).into(holder.appImage);}
@Override
public int getItemCount() {
    return imagesModules.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
    public android.widget.ImageView appImage;
    public ViewHolder(View itemView) {
        super( itemView );
        appImage = (android.widget.ImageView) itemView.findViewById( R.id.imagegallary);
     }}}

这是片段

public class ImageViewFragment extends Fragment {
List<ImagesModule> imagesModules;


List<ImagesModule> imagesModule;
RecyclerView AppRecyclerView;
RecyclerView.Adapter imageRecyclerViewadapter;
String jsonUrl = "https://itunes.apple.com/jo/rss/topfreeapplications/limit=50/json";
RequestQueue requestQueue;
private RecyclerView.LayoutManager mLayoutManager;
public ImageViewFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_image_view, container, false);
}
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    AppRecyclerView = (RecyclerView) getView().findViewById(R.id.imageRecyclerView);
    imagesModule = new ArrayList<>();
    imagesModules = new ArrayList<>();
    JsonAppShowData();
}
public void JsonAppShowData() {
    final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( jsonUrl, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                ImagesModule imagesModule = new ImagesModule();

                JSONArray jsonArray = response.getJSONObject("feed").getJSONArray("entry");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONArray imageArray = response.getJSONObject("feed").getJSONArray("entry").getJSONObject(i).getJSONArray("im:image");
                    for (int j =0; j < imageArray.length(); j++) {
                        ArrayList listTest = new ArrayList( );
                        String image = imageArray.getJSONObject(j).getString("label");
                        listTest.add(image);
                        imagesModule.setListTest(listTest);
                        imagesModules.add(imagesModule);}}

                imageRecyclerViewadapter = new ImageViewAdapter(imagesModules,getContext());
                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);}}

推荐答案

您将需要List<String> imageUrls保存要由毕加索查看的URL,而无需特殊对象.要解决此问题,请将您的json解析代码更改为

You would need a List<String> imageUrls to save urls to be viewed by picasso, no need for special object for that. To solve it, change your json parsing code to

try {
    JSONArray entries = response.getJSONObject("feed").getJSONArray("entry");
    int count = entries.length();
    for (int i = 0; i < count; i++) {
        JSONObject imageJson = entries.getJSONObject(i).getJSONObject("im:image");
        // in case you want to get image with height 53
        String imageUrl = imageJson.getJSONObject("0").getString("label");

        // String imageUrl = imageJson.getJSONObject("1").getString("label"); height 75
        // String imageUrl = imageJson.getJSONObject("2").getString("label"); height 100

        // add urls to the list
        imageUrls.add(imageUrl);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

然后将您的适配器更改为具有List<String> imageUrls而不是List<ImagesModule> imagesModule并将已解析的列表传递给构造函数.最后,在您的适配器onBindViewHolder()中将毕加索代码更改为

then change your adapter to have a List<String> imageUrls instead of List<ImagesModule> imagesModule and pass the parsed list to the constructor. Finally in your adapter onBindViewHolder() change the picasso code to

Picasso.with(context).load(imageUrls.get(position)).into(holder.appImage);

希望有帮助

这篇关于将来自Json的数据解析为毕加索的图像集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆