如何在另一个JSON数组中解析JSON数组并显示Clicked项 [英] How to parse JSON Array inside another JSON Array and display the Clicked item

查看:157
本文介绍了如何在另一个JSON数组中解析JSON数组并显示Clicked项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有这个Json数据一个链接

hello i have this Json Data a link

我这样解析

当我单击图像时,我想解析JsonArray以便将图像以这种方式在回收站视图中显示为piccaso

when i click into the image i want to parsing the JsonArray for the image to piccaso in recycler view like this ,

但是我得到了所有与这张图片一样的图片

but i get all image like this picture

我如何解决此问题并仅获取我单击的项目的图像???

how i can solve this problem and get just the images for the item which i clicked ???

这是我的模块:

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 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();
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONArray imageArray = response.getJSONObject(feedKey).getJSONArray(entryKey).getJSONObject(i).getJSONArray(imageKey);
                    for (int j = 0; j < imageArray.length(); j++) {
                        String image = imageArray.getJSONObject(j).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;


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) {

    Picasso.with(context).load(imageUrl.get(position)).into(holder.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);


    }}}

这是具有图像视图的活动,单击

and this is the activity that have the image view where click

public class ListViewDetailsFragment extends Fragment {
ImageView AppImage;
TextView AppName,AppArtist,AppContentType,AppRights,AppCategory,AppRealseDate,AppSammary;
ImageButton AppLink;
Context context;
public ListViewDetailsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_list_view_details, container, false);}
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    AppImage = (ImageView) getView().findViewById(R.id.imageView);
    AppName = (TextView) getView().findViewById(R.id.textname);
    AppArtist = (TextView) getView().findViewById(R.id.textartest);
    AppContentType = (TextView) getView().findViewById(R.id.textcontent);
    AppRights = (TextView) getView().findViewById(R.id.textrights);
    AppCategory = (TextView) getView().findViewById(R.id.textCategory);
    AppRealseDate = (TextView) getView().findViewById(R.id.textRelease);
    AppSammary = (TextView) getView().findViewById(R.id.textSummary);
    AppLink = (ImageButton) getView().findViewById(R.id.imageButton);
    String name = getActivity().getIntent().getExtras().getString("App_name");
    final String image = getActivity().getIntent().getExtras().getString("App_image");
    String artist = getActivity().getIntent().getExtras().getString("App_artist");
    String contentType = getActivity().getIntent().getExtras().getString("App_ContentType");
    String rights = getActivity().getIntent().getExtras().getString("App_Rights");
    String category = getActivity().getIntent().getExtras().getString("App_Category");
    String realse = getActivity().getIntent().getExtras().getString("App_ReleaseDate");
    final String link = getActivity().getIntent().getExtras().getString("App_link");
    String sammary = getActivity().getIntent().getExtras().getString("App_summary");
    AppName.setText(name);
    AppArtist.setText(artist);
    AppContentType.setText(contentType);
    AppRights.setText(rights);
    AppCategory.setText(category);
    AppRealseDate.setText(realse);
    AppSammary.setText(sammary);
    Picasso.with(context).load(image).into(AppImage);
    AppLink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity().getBaseContext(),
                    WebView.class);
            intent.putExtra("App_link", link);
            getActivity().startActivity(intent);}});
    AppImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String id = (String) view.getTag();
            Intent intent = new Intent(getActivity().getBaseContext(), ImageList.class);
            intent.putExtra("id", id);
            getActivity().startActivity(intent);
        }});}}

推荐答案

json数据具有指向三个不同大小图像的链接,在我看来,您正在请求提取所有三个图像.如果您不希望同时使用这三种图像,只需选择所需的尺寸(索引0、1或2),然后仅拉出该图像即可.

The json data has links to three different sized images, it looks to me like you are requesting that all three images are fetched. If you don't want all three, just select the size you want (index 0, 1 or 2) and pull that image only.

JSONArray imageArray = response.getJSONObject(feedKey).getJSONArray(entryKey).getJSONObject(i).getJSONArray(imageKey);
for (int j = 0; j < imageArray.length(); j++) {
   String image = imageArray.getJSONObject(j).getString(labelKey).toString();
   imageUrls.add(image);
   ...
}

样本数据:

"im:image":[  
               {  
                  "label":"http://is1.mzstatic.com/image/thumb/Purple20/v4/87/35/82/87358231-ce91-3d14-b306-95888c23db3c/mzl.gdgtivnk.png/53x53bb-85.png",
                  "attributes":{  
                     "height":"53"
                  }
               },
               {  
                  "label":"http://is5.mzstatic.com/image/thumb/Purple20/v4/87/35/82/87358231-ce91-3d14-b306-95888c23db3c/mzl.gdgtivnk.png/75x75bb-85.png",
                  "attributes":{  
                     "height":"75"
                  }
               },
               {  
                  "label":"http://is3.mzstatic.com/image/thumb/Purple20/v4/87/35/82/87358231-ce91-3d14-b306-95888c23db3c/mzl.gdgtivnk.png/100x100bb-85.png",
                  "attributes":{  
                     "height":"100"
                  }
               }

更新9/13/2016

基于我的想法,我仍然认为您的问题出在jsonAppShowData()方法中的嵌套循环中.尽管您似乎确实从上一活动中传入了某种形式的ID,但我看不到您在for循环中的任何地方都过滤了该ID.您需要将外部for循环限制为仅希望显示的项目.

Based on what I think you are after, I still think your problem is in your nested for loop in the jsonAppShowData() method. Though you do appear to pass some sort of ID extra in from the previous activity, I don't see you filtering on that item ID anywhere in the for loop. You need to restrict the outer for loop to only the item you wish to show.

对于图像缩放问题,可能是布局的设置方式,也可以尝试一些Picasso

For the image scaling issue, it may either be how your layout is setup or you can try some of the Picasso resize calls.

这篇关于如何在另一个JSON数组中解析JSON数组并显示Clicked项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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