Tổng hợp "Asynctask get image from internet bên trong listview custom adapter" làm thế nào ?

Thảo luận trong 'Thảo luận về lập trình Android' bắt đầu bởi BachCao, 12/9/16.

  1. BachCao

    BachCao New Member

    Lượt xem: 211
    Asynctask get image from internet bên trong listview custom adapter, làm sao để làm dc các bác nhỉ, e làm ra đc hình ảnh rồi mà nó lại bị trùng lặp hình ảnh.
    Thế mới nhọ các bác à.

    Mã:
    public class ExchangeAdapter extends ArrayAdapter<Items> {
     
    	Activity context;
    	int resource;
    	ArrayList<Items> objects;
    	Bitmap imgBitmap;
     
    	public ExchangeAdapter(Activity context, int resource, ArrayList<Items> objects) {
    		super(context, resource, objects);
    		this.context = context;
    		this.resource = resource;
    		this.objects = objects;
    	}
     
    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		LayoutInflater inflater = this.context.getLayoutInflater();
    		View row = inflater.inflate(this.resource, null);
     
    		Items exchange = this.objects.get(position);
     
    		ImageView lvPhoto = (ImageView) row.findViewById(R.id.imgPhoto);
    		TextView txtCountry = (TextView) row.findViewById(R.id.txtCountry);
    		TextView txtBuyTM = (TextView) row.findViewById(R.id.txtBuyTM);
    		TextView txtBuyCK = (TextView) row.findViewById(R.id.txtBuyCK);
    		TextView txtSellTM = (TextView) row.findViewById(R.id.txtSellTM);
    		TextView txtSellCK = (TextView) row.findViewById(R.id.txtSellCK);
     
    		lvPhoto.setImageBitmap(imgBitmap);
    		txtCountry.setText(exchange.getType());
    		txtBuyTM.setText(exchange.getMuatienmat());
    		txtBuyCK.setText(exchange.getMuack());
    		txtSellTM.setText(exchange.getBantienmat());
    		txtSellCK.setText(exchange.getBanck());
     
    		ImgTask imgTask = new ImgTask();
    		imgTask.execute(exchange.getImageurl());
     
    		return row;
    	}
     
    	class ImgTask extends AsyncTask<String, Void, Bitmap> {
    		@Override
    		protected void onPreExecute() {
    			super.onPreExecute();
    		}
     
    		@Override
    		protected void onPostExecute(Bitmap bitmap) {
    			super.onPostExecute(bitmap);
    			imgBitmap = bitmap;
    		}
     
    		@Override
    		protected void onProgressUpdate(Void... values) {
    			super.onProgressUpdate(values);
    		}
     
    		@Override
    		protected Bitmap doInBackground(String... params) {
    			Bitmap bitmap = null;
     
    			try {
    				String link = params[0];
    				URL url = new URL(link);
    				HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    				connection.setRequestMethod("GET");
    				connection.setRequestProperty("Accept", "*/*");
    				bitmap = BitmapFactory.decodeStream(connection.getInputStream());
     
    				return bitmap;
     
    			} catch (Exception e) {
    				Log.e("ERROR_BITMAP", e.toString());
    			}
     
    			return bitmap;
    		}
    	}
    }
    

Chia sẻ trang này