[android]分析图像从 Json 数据到真迹图像列表loader

标签: json Android
发布时间: 2016/10/10 21:05:43
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有这个Json 数据

和我的容貌在中对此匹配此 json 的数据

输入图像描述在这里

现在我不会当点击图像的 application ,打开另一个activity和在 json 中显示"im:image"Jsonarray 中的形象 ! 这段代码给我所有的图片,但我不会只是我的图像单击

这是我的模块

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

Adapter

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() ).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);
    }}}

这是 Fragment '

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(0).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);    }}

解决方法 1:

您正在试图加载完整 list 边学字符串 appShowModule.getAllimage() 它是错误的。 试着做以下。

Picasso.with(context).load(appShowModule.getAllimage().get(position)).into(holder.appImage);
赞助商