将JsonArray解析为Picasso库 [英] parsing JsonArray to Picasso library

查看:61
本文介绍了将JsonArray解析为Picasso库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 Json数据

并且我将此json中的数据赞为this,

and i prasing the data in in this json to this ,,

现在,我想要在用户单击应用程序的图像时打开另一个具有Recyceler视图的活动,并在json的"im:image" json数组中显示图像!这段代码为我提供了所有图像,但我不会只是单击任何想法的图像?

now i want when user click on the image of the application open another activty which have a recyceler view and display the images in the "im:image" json array in the json ! this code give to me all images ,but i wont just the image thats i clicked any idea ??

这是我的模块

public class AppShowModule{

 private List<String> Allimage = new ArrayList<String>();
public List<String> getAllimage() {
    return Allimage;}
public void setAllimage(List<String> allimage) {
    Allimage = allimage;}

回收站视图适配器

public class ImageListAdapter extends RecyclerView.Adapter<ImageListAdapter.ViewHolder> {
List<AppShowModule> appShowModules;
List<AppShowModule> imageUrls ;

Context context;
public ImageListAdapter(List<AppShowModule> appShowModules, Context context){
    super();
    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) {
    final AppShowModule appShowModule = appShowModules.get( position );
    Picasso.with(context).load(appShowModule.getAllimage().get(position)).into(holder.appImage);

}
@Override
public int getItemCount() {
    return appShowModules.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView appImage;
    public ViewHolder(View itemView) {
        super(itemView);
        appImage = (ImageView) itemView.findViewById( R.id.appImage);
    }}}

这是片段

public class ImageListFragment extends Fragment {

List<AppShowModule> appShowModules;
Context context;
List<AppShowModule> imagesModule;
RecyclerView AppRecyclerView;
ImageView Imageview;
RecyclerView.Adapter imageRecyclerViewadapter;
List<String> imageUrls;
String feedKey = "feed";
String entryKey = "entry";
String nameKey = "im:name";
String imageKey = "im:image";
String labelKey = "label";
String artistKey = "im:artist";
String contentTypeKey = "im:contentType";
String attribueKey = "attributes";
String rightsKey = "rights";
String categoryKey = "category";
String relaseDateKey = "im:releaseDate";
String linkKey = "link";
String hrefKey = "href";
String summaryKey = "summary";
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("feed").getJSONArray( "entry" );
                AppShowModule appShowModule = new AppShowModule();
                for (int i = 0; i < jsonArray.length(); i++) {

                    String image = response.getJSONObject(feedKey).getJSONArray(entryKey).getJSONObject(i).getJSONArray(imageKey).getJSONObject(0).getString(labelKey).toString();
                    imageUrls.add(image);
                    appShowModule.setAllimage(imageUrls);
                    appShowModules.add(appShowModule);
                }
                imageRecyclerViewadapter = new ImageListAdapter(appShowModules,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);    }}

推荐答案

在这里,您要通过执行以下操作从每个数组中获取第一个图像

Here you are fetching first image from each array by doing following

.... getJSONArray(entryKey).getJSONObject(i).getJSONArray(imageKey).getJSONObject(0)...

....getJSONArray(entryKey).getJSONObject(i).getJSONArray(imageKey).getJSONObject(0)...

您需要从数组中获取与所选图像相关的所有图像,因此要在内部图像数组im:image上而不是在外部主数组entry上迭代数组.请按照以下步骤

whereas you need to fetch all the images related to selected image from the array so iterate your array on the inner image array im:image, not on outer main array entry. follow these steps

  1. 只要您单击图像项目,然后获取该项目的ID.
  2. 在外部数组entry上迭代,并检查单击的项目ID在数组entry中是否匹配.
  3. 当它与id匹配时,然后对内部数组im:immage进行迭代,并将这些图像存储在列表中.并将该列表应用于您的recyclerview.
  1. Whenever you click on the image item then get the id of that item.
  2. iterate on the outer array entry and check if the clicked items id matches in the array entry.
  3. when it matches id then iterate on inner array im:immage and store those images in your list. And apply that list to your recyclerview.

希望这会有所帮助.

这篇关于将JsonArray解析为Picasso库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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