我在代码2下面有东西,Volley请求设置图像,数组列表适配器显示静态图像.
我需要帮助将排球请求添加到数组列表中,我希望排球图像显示在arrayList中
请问该怎么办?
(请注意,我已经有一个PHPscript,可以在Json中向我发送图像的路径URL)
代码:main_activity
protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); RequestQueue mRequestQueue; final String IMAGE_URL = "http://developer.androID.com/images/training/system-ui.png"; mNetworkImageVIEw = (NetworkImageVIEw) findVIEwByID(R.ID.networkImageVIEw);//Instantiate the cacheCache cache = new diskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap//Set up the network to use httpURLConnection as the http clIEnt.Network network = new BasicNetwork(new HurlStack());//Instantiate the RequestQueue with the cache and network.mRequestQueue = new RequestQueue(cache, network);//Start the queuemRequestQueue.start();ImageLoader mImageLoader = new ImageLoader(mRequestQueue, new LruBitmapCache( LruBitmapCache.getCacheSize(mContext)));mNetworkImageVIEw.setimageUrl(IMAGE_URL, mImageLoader);Customlistadapter adapter=new Customlistadapter(this, itemname, imgID);List=(ListVIEw)findVIEwByID(R.ID.List);List.setAdapter(adapter);List.setonItemClickListener(new OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { // Todo auto-generated method stub String Slecteditem= itemname[+position]; Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show(); }});}
arraylistadapter
包com.example.p;
import androID.app.Activity;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ArrayAdapter;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;public class Customlistadapter extends ArrayAdapter<String> { private final Activity context; private final String[] itemname; private final Integer[] imgID; public Customlistadapter(Activity context, String[] itemname, Integer[] imgID) { super(context, R.layout.myList, itemname); // Todo auto-generated constructor stub this.context=context; this.itemname=itemname; this.imgID=imgID; } public VIEw getVIEw(int position,VIEw vIEw,VIEwGroup parent) { LayoutInflater inflater=context.getLayoutInflater(); VIEw rowVIEw=inflater.inflate(R.layout.myList, null,true); TextVIEw txtTitle = (TextVIEw) rowVIEw.findVIEwByID(R.ID.item); ImageVIEw imageVIEw = (ImageVIEw) rowVIEw.findVIEwByID(R.ID.icon); TextVIEw extratxt = (TextVIEw) rowVIEw.findVIEwByID(R.ID.textVIEw1); txtTitle.setText(itemname[position]); imageVIEw.setimageResource(imgID[position]); extratxt.setText("Description "+itemname[position]); return rowVIEw; };}
和Lrubitmapcache
package com.example.p;import androID.content.Context;import androID.graphics.Bitmap;import androID.support.v4.util.LruCache;import androID.util.displayMetrics;import com.androID.volley.toolBox.ImageLoader.ImageCache;public class LruBitmapCache extends LruCache<String, Bitmap> implements ImageCache { public LruBitmapCache(int maxSize) { super(maxSize); } public LruBitmapCache(Context ctx) { this(getCacheSize(ctx)); } @OverrIDe protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight(); } @OverrIDe public Bitmap getBitmap(String url) { return get(url); } @OverrIDe public voID putBitmap(String url, Bitmap bitmap) { put(url, bitmap); } // Returns a cache size equal to approximately three screens worth of images. public static int getCacheSize(Context ctx) { final displayMetrics displayMetrics = ctx.getResources(). getdisplayMetrics(); final int screenWIDth = displayMetrics.wIDthPixels; final int screenHeight = displayMetrics.heightPixels; // 4 bytes per pixel final int screenBytes = screenWIDth * screenHeight * 4; return screenBytes * 3; }}
解决方法:
在其他帖子中,您需要我的帮助.
我的图像来自作为Json字符串Base64编码的其余服务器.然后我做下一个:
public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { final ObjectLogados o = items.get(position); final VIEwHolder holder; VIEw vi = convertVIEw; if (vi == null){ LayoutInflater inflater = context.getLayoutInflater(); vi = inflater.inflate(R.layout.singlerowlog, null);// e' preciso fazer o inflate pois a vIEw nao ainda nao esta em activity alguma holder = new VIEwHolder(); holder.ivimgMedLog= (ImageVIEw) vi.findVIEwByID(R.ID.ivp_imagemedlog) ; holder.tv_usuario = (TextVIEw) vi.findVIEwByID(R.ID.tv_hnomemedlogado); vi.setTag(holder); } else { holder = (VIEwHolder) convertVIEw.getTag(); } holder.tv_usuario.setText(o.getUsuario()); String imageKey="MED"+String.valueOf(o.getNr_usuario()); final Bitmap bitmap = Common.getInstance().getBitmapFromMemCache(imageKey); if (bitmap != null) { holder.ivimgMedLog.setimageBitmap(bitmap); }else { holder.ivimgMedLog.setimageDrawable(context.getResources() .getDrawable(R.drawable.empty)); downloadimage(holder.ivimgMedLog,o.getNr_usuario()); } return vi; } static class VIEwHolder { TextVIEw tv_usuario; ImageVIEw ivimgMedLog; } private voID downloadimage(final ImageVIEw imageVIEw,final int nr){ //final ImageVIEw imageVIEw=imV; String url=Common.getServerURL() + "\"RetrIEveImageStaff\""; JsONArray ja = new JsONArray(); ja.put(nr); JsONObject JsonBody = new JsONObject(); try { JsonBody.put("_parameters", ja); } catch (JsONException e) { e.printstacktrace(); } JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, url, JsonBody, new Response.Listener<JsONObject>() { @OverrIDe public voID onResponse(JsONObject response) { JsONObject anEntry = null, entrIEs = null; String ims = ""; try { // Todo auto-generated method stub JsONArray LResult = response.getJsONArray("result"); String s = LResult.get(0).toString(); anEntry = new JsONObject(s); ims = anEntry.getString("imagem"); } catch (JsONException e) { // Todo auto-generated catch block e.printstacktrace(); } Bitmap decodedByte = null; try { if (!TextUtils.isEmpty(ims)) { byte[] decodedString = Base64.decode(ims, Base64.DEFAulT); decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); if (imageVIEw != null) { if (decodedByte != null) { imageVIEw.setimageBitmap(decodedByte); Common.getInstance().addBitmapToMemoryCache("MED"+String.valueOf(nr), decodedByte); } else { imageVIEw.setimageDrawable(imageVIEw.getContext().getResources() .getDrawable(R.drawable.empty)); } } } else { imageVIEw.setimageDrawable(imageVIEw.getContext().getResources() .getDrawable(R.drawable.empty)); } }catch (Exception e){ e.printstacktrace(); } } }, new Response.ErrorListener() { @OverrIDe public voID one rrorResponse(VolleyError error) { // Todo auto-generated method stub //Log.v(TAG, error.getMessage()); Toast.makeText(context, "Erro adquirindo imagem...." + error.getMessage(), Toast.LENGTH_SHORT).show(); error.printstacktrace(); } }); jr.setRetryPolicy(new DefaultRetryPolicy(Common.DEFAulT_TIMEOUT_MS, Common.DEFAulT_MAX_RETRIES, DefaultRetryPolicy.DEFAulT_BACKOFF_MulT)); Common.getInstance().addToReqQueue(jr, "jreq"); //reqQueue.add(jr); }
您可以更改使用排球的请求并获取图像.因此,您可以将https://developer.android.com/training/volley/request.html#request-image中描述的代码更改为类似于以下内容的代码:
ImageVIEw mImageVIEw;String url = "http://i.imgur.com/7spzG.png";mImageVIEw = (ImageVIEw) findVIEwByID(R.ID.myImage);...// RetrIEves an image specifIEd by the URL, displays it in the UI.ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() { @OverrIDe public voID onResponse(Bitmap bitmap) { mImageVIEw.setimageBitmap(bitmap); } }, 0, 0, null, new Response.ErrorListener() { public voID one rrorResponse(VolleyError error) { mImageVIEw.setimageResource(R.drawable.image_load_error); } });// Access the RequestQueue through your singleton class.MySingleton.getInstance(this).addToRequestQueue(request);
总结 以上是内存溢出为你收集整理的android-如何在arraylist中添加排球图像请求?全部内容,希望文章能够帮你解决android-如何在arraylist中添加排球图像请求?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)