how do i display image using the imagepath fetched from mysql database into gridview. i tried lot but unable to do.
this is my asynctask code:
public class sampling_info extends AsyncTask<String, String, JSONObject> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SamplingActivity.this);
pDialog.setMessage("Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected JSONObject doInBackground(String... params) {
// TODO Auto-generated method stub
String doc = dcname.toString();
ArrayList<NameValuePair> namevaluepair = new ArrayList<NameValuePair>();
namevaluepair.add(new BasicNameValuePair("docname", doc));
JSONObject json = jParser.makeHttpRequest(URL_SAMPLING, "POST",
namevaluepair);
return json;
}
protected void onPostExecute(JSONObject json) {
// dismiss the dialog once done
pDialog.dismiss();
if (json != null) {
try {
JSONArray jarray = json.getJSONArray("product_entered");
Log.d("jarray in sampling : ", "" + jarray.toString());
for (int i = 0; i < jarray.length(); i++) {
JSONObject jobj = jarray.getJSONObject(i);
String docname = jobj.getString("docname");
String imagep= jobj.getString("image_path");
GridItem gItem= new GridItem();
gItem.setTitle("sagar");
gItem.setImage(imagep);
mgrid.add(gItem);
Log.d("jobj : ", "" + jobj);
Log.d("jobj docname", "" + docname);
}
} catch (Exception e) {
e.printStackTrace();
}
gridAdapter = new GridViewAdapter(SamplingActivity.this, R.layout.grid_item_layout,mgrid);
gridview.setAdapter(gridAdapter);
}
}
i this asynctask am fetching imagepath from database and storing it in imagep variable and passing it to GridItem class
This is my griditem class :
public class GridItem {
private Bitmap image;
private String title,image1;
public GridItem(Bitmap image) {
this.image=image;
}
public Bitmap getImage() {
return image;
}
public Bitmap setImage(String image) { // this is where am trying to set my image but can't do.
try {
} catch (Exception e) {
}
return null;
}
And this is my adapter class :
public GridViewAdapter(Context mContext, int layoutResourceId,
ArrayList<GridItem> mGridData) {
super(mContext, layoutResourceId, mGridData);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.mGridData = mGridData;
}
/**
* Updates grid data and refresh grid items.
*
* #param mGridData
*/
public void setGridData(ArrayList<GridItem> mGridData) {
this.mGridData = mGridData;
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
//ViewHolder holder;
ImageView imageView = (ImageView) row
.findViewById(R.id.grid_item_image);
if (row == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
// holder = new ViewHolder();
//imageView = (ImageView) convertView;
TextView titleTextView = (TextView) row
.findViewById(R.id.grid_item_title);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
}
GridItem item = mGridData.get(position);
imageView.setImageBitmap(item.getImage());
return row;
}
I suggest you to use Universal Image Loader
First of all, include last version of jar file from 'lib' directory to your project
in Adapter :
ImageLoader imageLoader = null;
DisplayImageOptions options;
in Adapter's Contractor :
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
/*.displayer(new RoundedBitmapDisplayer((int) 27.5f))*/
.showImageOnLoading(R.drawable.thumb_square)
.showImageForEmptyUri(R.drawable.thumb_square)
.showImageOnFail(R.drawable.thumb_square)
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(true)
.imageScaleType(ImageScaleType.NONE)
.bitmapConfig(Bitmap.Config.RGB_565)
/*.postProcessor(new BitmapProcessor() {
#Override
public Bitmap process(Bitmap bitmap) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
float r = ((float)h / (float)w);
ExceptionHelpers.dLog("POST_PROCESSOR", "width : " + w + " , height : " + h + " , R1 : " + r + " , W : " + UIHelpers.width + " , Percent W : " + UIHelpers.setPercentW(r));
return Bitmap.createScaledBitmap(bitmap, UIHelpers.width, UIHelpers.setPercentW(r), false);
}
})*/
.build();
and getView :
String imgURL = "http://www.website.com/images/image_path.png";
imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
imageLoader.displayImage(imgURL, holder.img_view, options);
First of all dont store the bitmap in modelClass, Alternatively you can add sd card path or live url of an image(http://...)
Loading the image:
-- I suggest you to use Glide library
Code sample:
Glide.with(context)
.load(url) // pass your image url
.centerCrop()
.placeholder(R.drawable.loading_spinner)
.crossFade() //animate the image
.into(myImageView);
Related
Hello I am using listview and display images from server into it.
But the problem is that all loaded images are displayed in last item one by one instead display on respective position.
please help me to display that images in respective position
public class Offer_adapter extends ArrayAdapter<String> {
Context context1;
String[] offer_title;
String[] offerimg1;
String[] mrp;
String[] offerprice;
String[] you_save;
String[] imgURLArray;
Bitmap bitmap;
ImageView offerimg;
int a;
LayoutInflater inflater1;
public Offer_adapter(Context context1, String[] offer_title, String[] offerimg1, String[] mrp, String[] you_save, String[] offerprice) {
super(context1, R.id.offer_list, offer_title);
this.context1 = context1;
this.offer_title = offer_title;
this.offerimg1 = offerimg1;
this.mrp = mrp;
this.offerprice = offerprice;
this.you_save = you_save;
}
private static class ViewHolder {
String offerimg;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
inflater1 = (LayoutInflater) context1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater1.inflate(R.layout.offer_list, null);
viewHolder = new ViewHolder();
}
android.util.Log.v("abhi", "" + position);
imgURLArray = new String[position + 1];
for (int i = 0; i <= position; i++) {
android.util.Log.v("abhijit", "" + position);
imgURLArray[i] = "http://www.surun.co/preost/mod_offer/images/" + offerimg1[position];
android.util.Log.v("abhi", "" + imgURLArray[position]);
}
a=position;
viewHolder = (ViewHolder) convertView.getTag();
TextView offertitle = (TextView) convertView.findViewById(R.id.ofrtitle);
TextView offermrp = (TextView) convertView.findViewById(R.id.offeroriginal);
offermrp.setPaintFlags(offermrp.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
TextView offersave = (TextView) convertView.findViewById(R.id.saveoffer);
TextView ofrprice = (TextView) convertView.findViewById(R.id.priceoffer);
offerimg = (ImageView) convertView.findViewById(R.id.ofr_img);
offertitle.setText(offer_title[position]);
offermrp.setText("Original Price: \u20B9" + mrp[position]);
offersave.setText("You Save: \u20B9" + you_save[position]);
ofrprice.setText("Offer Price: \u20B9" + offerprice[position]);
// Bitmap imageBitmap = null;
new DownloadAsyncTask().execute(imgURLArray[position]);
Log.v("abhi","async");
return convertView;
}
private class DownloadAsyncTask extends AsyncTask<String, String, Bitmap> {
protected Bitmap doInBackground(String... args) {
try {
Log.v("abhi","in do background");
bitmap = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent());
bitmap = Bitmap.createScaledBitmap(bitmap, 270, 375, true);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (bitmap !=null) {
offerimg.setImageBitmap(bitmap);
} else {
offerimg.setImageResource(R.drawable.nooffer);
}
}
}
}
All images are shown in last item one after another. I just want to show it on respective position.
you should use lazyloading instead of downloading image as bitmap.
Bitmap will create problem sometime or will give you outofmemory error in some devices
There are lots of image loading library available for android.
Have a look at these
https://github.com/square/picasso
https://github.com/nostra13/Android-Universal-Image-Loader
https://code.google.com/p/android-query/wiki/ImageLoading
https://android.googlesource.com/platform/frameworks/volley
https://github.com/koush/UrlImageViewHelper
https://github.com/novoda/image-loader
I am using universalimageloader in my project to Show many images
When I try to show images it is too Slow, After commented Line Of Display Images it shows Rows as well and not Slow
the Code I tried is :
public class GridAdapterMa extends BaseAdapter {
ArrayList<JobItem> objects = new ArrayList<JobItem>();
int size = 0;
private String TAG = GridAdapterMa.class.getSimpleName();
ImageLoader imageLoader;
DisplayImageOptions options;
ImageLoaderConfiguration config;
public GridAdapterMa(ArrayList<JobItem> objects) {
size = objects.size();
this.objects.addAll(objects);
ExceptionHelpers.dLog(TAG, "Constructor");
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.thumb_square)
.showImageForEmptyUri(R.drawable.thumb_square)
.showImageOnFail(R.drawable.thumb_square)
// .cacheInMemory(false)
.cacheOnDisc(true)
.resetViewBeforeLoading(true)
// .delayBeforeLoading(100)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new SimpleBitmapDisplayer())
/*.preProcessor(new BitmapProcessor() {
#Override
public Bitmap process(Bitmap bitmap) {
int w = (int) (UIHelpers.width * 0.50);
float r = ((float) bitmap.getHeight() / (float) bitmap.getWidth());
int h = (int) (w * r);
ExceptionHelpers.dLog(TAG, "Adapter Width : " + w + " Height : " + h + " R : " + r);
ExceptionHelpers.dLog(TAG, "Adapter Width : " + bitmap.getWidth() + " Height : " + bitmap.getHeight());
return Bitmap.createScaledBitmap(bitmap, w, h, false);
}
})*/
.build();
config = new ImageLoaderConfiguration.Builder(App.context)
.defaultDisplayImageOptions(options)
.memoryCacheSize(41943040)
.discCacheSize(104857600)
.threadPoolSize(10)
.build();
}
#Override
public int getCount() {
return size;
}
#Override
public JobItem getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
try {
if(convertView==null){
LayoutInflater vi = (LayoutInflater) App.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(UIHelpers.screen_density >= 4){
convertView = vi.inflate(R.layout.grid_ma_xl, null);
}else{
convertView = vi.inflate(R.layout.grid_ma_m, null);
}
holder = new ViewHolder();
holder.img =(ImageView) convertView.findViewById(R.id.img_image_gv_ma);
holder.imgVip = (ImageView) convertView.findViewById(R.id.img_vip_gv_ma);
holder.txtTitle = (TextView) convertView.findViewById(R.id.txt_title_gv_item);
holder.txtCat = (TextView) convertView.findViewById(R.id.txt_cat_gv_item);
holder.txtDesc = (TextView) convertView.findViewById(R.id.txt_desc_gv_item);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
Typeface tf = Typeface.createFromAsset(App.context.getAssets(), "b_yekan.ttf");
Typeface tf1 = Typeface.createFromAsset(App.context.getAssets(), "b_mitra.ttf");
holder.txtTitle.setTypeface(tf1);
holder.txtCat.setTypeface(tf);
holder.txtDesc.setTypeface(tf);
holder.txtTitle.setTextSize(UIHelpers.titleFont);
holder.txtCat.setTextSize(UIHelpers.descFont);
holder.txtDesc.setTextSize(UIHelpers.descFont);
if(objects.get(position).getjVip()==1) {
holder.imgVip.setVisibility(View.VISIBLE);
} else if (objects.get(position).getjVip()==0) {
holder.imgVip.setVisibility(View.INVISIBLE);
}
String imgURL = "";
imgURL = imgURL + objects.get(position).getjImage();
// ExceptionHelpers.dLog(TAG, "imgURL : '" + imgURL + "'");
/*if(imgURL.equals("http://www.yazdjobs.com/null") ||
imgURL.equals("http://www.yazdjobs.com/") ||
imgURL.equals("http://yazdjobs.com/null") ||
imgURL.equals("http://yazdjobs.com/")){
imgURL = "http://www.yazdjobs.com/wp-content/themes/yazdjobs/images/no-image.png";
}*/
// ExceptionHelpers.dLog(TAG, "imgURL : '" + imgURL + "'");
// ExceptionHelpers.dLog(TAG, "Position : "+position+" / "+objects.size());
// Show Images
// imageLoader.init(ImageLoaderConfiguration.createDefault(App.context));
imageLoader.init(config);
ImageAware imageAware = new ImageViewAware(holder.img, false);
imageLoader.displayImage(imgURL, imageAware, options);
/*Picasso.with(App.context)
.load(imgURL)
.placeholder(R.drawable.thumb_square)
.error(R.drawable.thumb_square)
.into(holder.img);*/
holder.txtTitle.setText(objects.get(position).getjTitle());
holder.txtCat.setText(objects.get(position).getjCatName());
if(UIHelpers.screen_density >= 4){
holder.txtDesc.setText(objects.get(position).getjDescLimit());
}else{
holder.txtDesc.setText(objects.get(position).getjDesc());
}
} catch (Exception e) {
ExceptionHelpers.catchMessage(e);
ExceptionHelpers.eLog(TAG, "Error in Grid Adapter >> " + e, e);
}
return convertView;
}
public class ViewHolder {
private ImageView img;
private ImageView imgVip;
private TextView txtTitle;
private TextView txtCat;
private TextView txtDesc;
}
}
I Even Tried ImageLoaderConfiguration But Not works To Me.
How To Fix It
After I decided to implement Universal Image Loader, because I had implemented a method that convert URL to Drawable, but since I don't know how many images it will return my SQLite query I decided to implement an Image Loader...
The thing is I'm stuck at the moment, cause I thought I did all what the GitHub say but at the time I load the Image it stays white and never loads.
On my Adapter class I've changed the line of the drawable as :
Picasso.with(context)
.load(Uri.parse(String.valueOf(item.icon)))
.resize(180, 180)
.placeholder(R.drawable.ic_launcher).into(viewHolder.ivIcon);
It works, beucase it shows yo me the ic_launcher icon... but never changes to the real image.
On my class where I fetch the data I have this (on my OnCreate()) :
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new MyAsyncTask().execute();
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
// progress.dismiss();
}
});
}
}).start();
}
Then I created an inner class where I fetch the data into my ListView... but it doesn't works. I don't know If I've to delte those methods since I've changed it to Picasso.
private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
}
#Override
protected Void doInBackground(Void... params) {
Conexion = new MarketSQLite(getActivity(), "market", null, 1);
mItems = new ArrayList<ListViewItem>();
db = Conexion.getReadableDatabase();
Cursor c;
c = db.rawQuery("Select NOM_OFER,PREU_OFERTA,DATA_F,FOTO,PERCENTDESCOMPTE from T_OFERTA", null);
c.moveToFirst();
if (c != null) {
do {
for (int i = 0; i < c.getColumnCount(); i++) {
Title = c.getString((c.getColumnIndex("NOM_OFER")));
Preu = c.getColumnIndex("PREU_OFERTA");
percent = c.getString((c.getColumnIndex("PERCENTDESCOMPTE")));
data_f = c.getString((c.getColumnIndex("DATA_F")));
URLTest = c.getString((c.getColumnIndex("FOTO")));
FOTO = Imagehandler(URLTest);
Log.e("", "" + c.getString(i));
// initialize and set the list adapter
// Toast.makeText(getActivity(), "Title" + Title + "Preu" + Preu + "Percent" + percent + "Cheese is " + data_f, Toast.LENGTH_LONG).show();
}
mItems.add(new ListViewItem(FOTO, Title, Preu.toString(), percent, data_f));
}while (c.moveToNext());
}
c.close();
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
myAdapter = new ListViewDemoAdapter(getActivity(), mItems);
setListAdapter(myAdapter);
}
}
Where ImageHandler is a method that I've created before this is :
protected Drawable Imagehandler(String url) {
try {
url=url.replaceAll(" ", "%20");
InputStream is = (InputStream)this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e)
{
System.out.println(url);
System.out.println("error at URI"+e);
return null;
}
catch (IOException e)
{
System.out.println("io exception: "+e);
System.out.println("Image NOT FOUND");
return null;
}
}
protected Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
I don't know why isn't the image loading on my ListView if it shows all of the rest of data...
Instead of Drawable, try to get url string in your adapter like
Change From
public ListViewItem(Drawable icon, String title, String precio, String descuento, String date) {
this.icon = icon;
this.title = title;
this.precio = precio;
this.descuento = descuento;
this.date = date;
}
To
public ListViewItem(String icon_url, String title, String precio, String descuento, String date) {
this.icon_url = icon_url;
this.title = title;
this.precio = precio;
this.descuento = descuento;
this.date = date;
}
and use Picasso where you are loading your imageview like this -
Picasso.with(context)
.load(icon_url))
.resize(180, 180)
.placeholder(R.drawable.ic_launcher).into(viewHolder.ivIcon);
1) Your ListViewItem class should be like this -
public class ListViewItem {
public final String icon; // the drawable for the ListView item ImageView
public final String title; // the text for the ListView item title
public final String precio; // the price for the ListView item
public final String descuento; // the price for the discount for the ListView item
public final String date; //the date for the sale for the ListView item
// the text for the ListView item description
public ListViewItem(String icon_url, String title, String precio, String descuento, String date) {
this.icon = icon_url;
this.title = title;
this.precio = precio;
this.descuento = descuento;
this.date = date;
}
}
2) ListViewDemoAdapterClass
public class ListViewDemoAdapter extends ArrayAdapter<ListViewItem> {
Context context;
public ListViewDemoAdapter(Context context, List<ListViewItem> items) {
super(context, R.layout.listview_item, items);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.listview_item, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
viewHolder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
viewHolder.tvDiscount = (TextView) convertView.findViewById(R.id.tvDiscount);
viewHolder.tvDate = (TextView) convertView.findViewById(R.id.tvDatas);
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
// update the item view
ListViewItem item = getItem(position);
Picasso.with(context)
.load(item.icon)
.resize(180, 180)
.placeholder(R.drawable.ic_launcher).into(viewHolder.ivIcon);
viewHolder.tvTitle.setText(item.title);
viewHolder.tvDiscount.setText(item.descuento);
viewHolder.tvPrice.setText(item.precio);
viewHolder.tvDate.setText(item.date);
return convertView;
}
private static class ViewHolder {
ImageView ivIcon;
TextView tvTitle;
TextView tvDiscount;
TextView tvPrice;
TextView tvDate;
}
}
ListFragment code, just add this
Cursor c;
c = db.rawQuery("Select
NOM_OFER,PREU_OFERTA,DATA_F,FOTO,PERCENTDESCOMPTE from T_OFERTA", null);
c.moveToFirst();
if (c != null) {
do {
for (int i = 0; i < c.getColumnCount(); i++) {
Title = c.getString((c.getColumnIndex("NOM_OFER")));
Preu = c.getColumnIndex("PREU_OFERTA");
percent = c.getString((c.getColumnIndex("PERCENTDESCOMPTE")));
data_f = c.getString((c.getColumnIndex("DATA_F")));
URLTest = c.getString((c.getColumnIndex("FOTO")));
Hope this helps :)
you just need to add your picasso code snippet as the following in your ImageHandler method and nothing else-
Picasso.with(context)
.load(url))
.resize(180, 180)
.placeholder(R.drawable.ic_launcher).into(your_imageview);
you don't need to download the image or make the bitmap or convert that into drawable to load from url. hope this helps you.
Here is how I set up the list view and how I get the image by downloading it.
Some variable explanation :
The PostItem is the model object that contain the data for a listview item
The ImageLoader is the async task class to download the image by getting the image url from PostItem
The problem are , the ordering of the image in the listview is incorrect , for example, the image should appear in 1st is appear in both 1st , 4th, and if I scroll , the display pattern change as well.
Also, I find the image are download again if I scroll, even I have check the imageView whether has drawable
Thanks for helping.
====================================================
Here is how I generate the listview:
static class ViewHolderItem {
TextView name;
TextView date;
ImageView img;
TextView msg;
TextView count;
ImageView likeBtn;
ImageView commentBtn;
ImageView shareBtn;
}
private class MyPostAdapter extends ArrayAdapter<PostItem> {
#Override
public boolean isEnabled(int position) {
return false;
}
public MyPostAdapter(Context context, int resource, List<PostItem> items) {
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolderItem viewHolder;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.post_item, parent, false);
viewHolder = new ViewHolderItem();
viewHolder.name = (TextView) v.findViewById(R.id.postName);
viewHolder.date = (TextView) v.findViewById(R.id.postDate);
viewHolder.img = (ImageView) v.findViewById(R.id.postImg);
viewHolder.msg = (TextView) v.findViewById(R.id.postMsg);
viewHolder.count = (TextView) v.findViewById(R.id.count);
viewHolder.likeBtn = (ImageView) v.findViewById(R.id.likeBtn);
viewHolder.commentBtn = (ImageView) v.findViewById(R.id.commentBtn);
viewHolder.shareBtn = (ImageView) v.findViewById(R.id.shareBtn);
v.setTag(viewHolder);
} else {
viewHolder = (ViewHolderItem) convertView.getTag();
}
final PostItem post = getItem(position);
if (post != null) {
viewHolder.name.setText(post.name);
try {
c.setTime(sdf.parse(post.createDate));
} catch (ParseException e) {
e.printStackTrace();
}
relative_date = DateUtils.getRelativeDateTimeString (ctx, c.getTimeInMillis() , DateUtils.MINUTE_IN_MILLIS,DateUtils.WEEK_IN_MILLIS, 0).toString();
viewHolder.date.setText(relative_date);
viewHolder.msg.setText(post.txtMsg);
viewHolder.count.setText(post.likeCount + " " + getString(R.string.pro_like) + " " + post.commentCount + " " + getString(R.string.reply));
if (post.isLike) {
viewHolder.likeBtn.setImageResource(R.drawable.like);
} else {
viewHolder.likeBtn.setImageResource(R.drawable.before_like);
}
if (!post.imageURL.equals("null") && viewHolder.img.getDrawable() == null ) {
new ImageLoader(ctx).execute(viewHolder.img,Constant.comment_imageFolder + post.imageURL);
} else {
viewHolder.img.setImageDrawable(null);
}
viewHolder.likeBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
new APIManager("like", ctx, Constant.likeAPI + "/"
+ post.commentID + "/" + userID, jsonListener,
getResources().getString(R.string.update_data));
}
});
viewHolder.commentBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<PostItem> filterReplyList = new ArrayList<PostItem>();
Intent i = new Intent(ctx, ReplyActivity.class);
i.putExtra("commentID", post.commentID);
// get reply list
for (PostItem reply : replyItemList) {
if (reply.postID.equals(post.commentID)
|| reply.commentID.equals(post.commentID)) {
filterReplyList.add(reply);
}
}
i.putExtra("replyItemList", filterReplyList);
startActivityForResult(i, 0);
}
});
viewHolder.shareBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
String data = "date: " + post.createDate + "\nmsg:" + post.txtMsg;
sendIntent.putExtra(Intent.EXTRA_TEXT, data);
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
});
}
return v;
}
}
And Here is the imageloader, take the imageview, url as input and put the bitmap in the imageview
public class ImageLoader extends AsyncTask<Object, Void, Bitmap> {
private static String TAG = "ImageLoader";
private InputStream input;
private ImageView view;
private ProgressBar loadingIcon;
private ListView myListView;
private String imageURL;
private Context ctx;
public ImageLoader(Context _ctx) {
ctx = _ctx;
}
#Override
protected Bitmap doInBackground(Object... params) {
try {
view = (ImageView) params[0];
// handle Chinese characters in file name
// String[] imgUrlArray = ((String) params[1]).split("/");
// String fileName = imgUrlArray[imgUrlArray.length - 1];
// String newfileName = URLEncoder.encode(fileName, "utf-8");
// imageURL = ((String) params[1]).replace(fileName, newfileName);
imageURL = ((String) params[1]);
if (params.length > 2 && (ProgressBar) params[2] != null)
loadingIcon = (ProgressBar) params[2];
URL url = new URL(imageURL);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
input = connection.getInputStream();
final BitmapFactory.Options options = new BitmapFactory.Options();
BufferedInputStream bis = new BufferedInputStream(input, 4*1024);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte)current);
}
byte[] imageData = baf.toByteArray();
BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
options.inJustDecodeBounds = true;
options.inSampleSize = 2;
options.inJustDecodeBounds = false;
return BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
try {
if (input != null)
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
protected void onPostExecute(Bitmap result) {
if (result != null && view != null) {
if (loadingIcon != null)
loadingIcon.setVisibility(View.GONE);
view.setVisibility(View.VISIBLE);
view.setImageBitmap(result);
}
}
Updated code (implement volley library):
static class ViewHolderItem {
TextView name;
TextView date;
NetworkImageView img;
TextView msg;
TextView count;
ImageView likeBtn;
ImageView commentBtn;
ImageView shareBtn;
}
private class MyPostAdapter extends ArrayAdapter<PostItem> {
#Override
public boolean isEnabled(int position) {
return false;
}
public MyPostAdapter(Context context, int resource, List<PostItem> items) {
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolderItem viewHolder;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.post_item, parent, false);
viewHolder = new ViewHolderItem();
viewHolder.name = (TextView) v.findViewById(R.id.postName);
viewHolder.date = (TextView) v.findViewById(R.id.postDate);
viewHolder.img = (NetworkImageView) v.findViewById(R.id.postImg);
viewHolder.msg = (TextView) v.findViewById(R.id.postMsg);
viewHolder.count = (TextView) v.findViewById(R.id.count);
viewHolder.likeBtn = (ImageView) v.findViewById(R.id.likeBtn);
viewHolder.commentBtn = (ImageView) v.findViewById(R.id.commentBtn);
viewHolder.shareBtn = (ImageView) v.findViewById(R.id.shareBtn);
v.setTag(viewHolder);
} else {
viewHolder = (ViewHolderItem) convertView.getTag();
}
final PostItem post = getItem(position);
if (post != null) {
viewHolder.name.setText(post.name);
try {
c.setTime(sdf.parse(post.createDate));
} catch (ParseException e) {
e.printStackTrace();
}
relative_date = DateUtils.getRelativeDateTimeString (ctx, c.getTimeInMillis() , DateUtils.MINUTE_IN_MILLIS,DateUtils.WEEK_IN_MILLIS, 0).toString();
viewHolder.date.setText(relative_date);
viewHolder.msg.setText(post.txtMsg);
viewHolder.count.setText(post.likeCount + " " + getString(R.string.pro_like) + " " + post.commentCount + " " + getString(R.string.reply));
if (post.isLike) {
viewHolder.likeBtn.setImageResource(R.drawable.like);
} else {
viewHolder.likeBtn.setImageResource(R.drawable.before_like);
}
if (!post.imageURL.equals("null")) {
viewHolder.img.setImageUrl(Constant.comment_imageFolder + post.imageURL, mImageLoader);
}
viewHolder.likeBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
new APIManager("like", ctx, Constant.likeAPI + "/"
+ post.commentID + "/" + userID, jsonListener,
getResources().getString(R.string.update_data));
}
});
viewHolder.commentBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<PostItem> filterReplyList = new ArrayList<PostItem>();
Intent i = new Intent(ctx, ReplyActivity.class);
i.putExtra("commentID", post.commentID);
// get reply list
for (PostItem reply : replyItemList) {
if (reply.postID.equals(post.commentID)
|| reply.commentID.equals(post.commentID)) {
filterReplyList.add(reply);
}
}
i.putExtra("replyItemList", filterReplyList);
startActivityForResult(i, 0);
}
});
viewHolder.shareBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
String data = "date: " + post.createDate + "\nmsg:" + post.txtMsg;
sendIntent.putExtra(Intent.EXTRA_TEXT, data);
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
});
}
return v;
}
For the task you are trying to do I would strongly recommend you to use Volley library.
Read from here
All you need to do is as below
mRequestQueue = Volley.newRequestQueue(context);
mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache());
mImageView.setImageUrl(BASE_URL + item.image_url, mImageLoader);
Where mImageView is com.android.volley.NetworkImageView instead of a regular ImageView.
Volley takes care of maintaining the cache and the ordering of the images.
if you scroll the listview you will get back recycled convertview, it is not null but it has incorrect imageview. convertView is a view thats created and recycled through scrolling the list. this view makes GC be called less and also save memory for you. it first assigned by your earliest items of list. after you scroll the list, for example item one of list disappears and you see item 15 the convertView of item one is passed again to you. in this time it is not null and it holdes the reference of last imageview, the imageview of item 1.
so this is your problem, you skipped assigning correct imageview to your viewHolder.img.
Ok, what should you do?
the best thing you can do is create in memory cache that holds your downloaded imageview by their URLs as keys of the cache. in getview you check the cache, if it has your URL of current imageview position read from it and set it to viewHolder.img else download the image from internet.
Golden rule is:
ALWAYS OVERWRITE VIEWHOLDER VALUES WITH VALUES OF YOUR ITEM AT INDEX POSITON THAT
GETVIEW PASSES TO YOU
how to create cache? look at Example LRU cache at
http://developer.android.com/training/volley/request.html
and if you want you can also use volley library instead.
if (!post.imageURL.equals("null") && viewHolder.img.getDrawable() == null ) {
new ImageLoader(ctx).execute(viewHolder.img,Constant.comment_imageFolder + post.imageURL);
}
I am guessing that the problem lies here. What happens when you get a recycled view which already has an image from the previous view it was used for? That explains why the image appears in both 1st and 4th position and the change in the display pattern when you scroll.
Get the point? Remove the viewHolder.img.getDrawable() == null and try. See if that helps.
I have a Custom ArrayAdapter that has a getview method, the thing is that method is being called for different views if i scroll down and up, and this make this views load again (e.g. downloading an image).
In my ListActivity class, i only call adapter = new Adapter... and list.setAdapter() , after i download and parse a json (im using AQuery library).
public class AdapterPostagem extends ArrayAdapter<Postagem>{
Context context;
LayoutInflater inflater;
boolean memCache = false;
boolean fileCache = true;
String BASE_URL = "https://graph.facebook.com/";
public AdapterPostagem(Context context, int textViewResourceId,
List<Postagem> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
inflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
this.context = context;
}
public class ViewHolder{
TextView postagem;
TextView likes;
ImageView foto;
View view;
TextView like;
TextView share;
ProgressBar pb;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
Postagem p = new Postagem();
p = getItem(position);
AQuery aq;
UrlImageViewHelper uHelper = new UrlImageViewHelper();
uHelper.setUseBitmapScaling(false);
if (convertView == null){
convertView = inflater.inflate(R.layout.postagem, parent, false);
Log.d("conv", "convertView_null");
Postagens posts = new Postagens();
Postagens.EndlessScrollListener end = posts.new EndlessScrollListener();
aq = new AQuery(convertView);
aq.scrolled(end);
holder = new ViewHolder();
holder.foto = (ImageView)convertView.findViewById(R.id.foto);
holder.postagem = (TextView)convertView.findViewById(R.id.postagem);
holder.like = (TextView)convertView.findViewById(R.id.likes);
holder.share = (TextView)convertView.findViewById(R.id.shares);
//holder.pb = (ProgressBar)convertView.findViewById(R.id.progress);
if (!p.getUrl_foto().equals("")){
getUrlPhoto(holder.foto, uHelper,p.getId(), aq, R.id.foto);
}else{
holder.foto.setVisibility(View.GONE);
}
holder.postagem.setText(p.getPostagem());
convertView.setTag(holder);
}else{
Log.d("conv", "convertView");
holder = (ViewHolder)convertView.getTag();
Postagens posts = new Postagens();
Postagens.EndlessScrollListener end = posts.new EndlessScrollListener();
aq = new AQuery(convertView);
aq.scrolled(end);
holder.foto = (ImageView)convertView.findViewById(R.id.foto);
holder.postagem = (TextView)convertView.findViewById(R.id.postagem);
if (!p.getId().equals("")){
getUrlPhoto(holder.foto, uHelper,p.getId(), aq, R.id.foto);
}else{
holder.foto.setVisibility(View.GONE);
}
//uHelper.setUrlDrawable(holder.foto, p.getUrl_foto());
setTextWithURL(holder.postagem, p.getPostagem());
//holder.postagem.setText(p.getPostagem());
//holder.likes.setText(Integer.toString(p.getLikes()));
}
return convertView;
}
public void setTextWithURL(TextView t, String s){
String [] parts = s.split("\\s");
String finals = "";
for( String item : parts ) try {
URL url = new URL(item);
// If possible then replace with anchor...
finals = finals + (""+ url + " " );
} catch (MalformedURLException e) {
// If there was an URL that was not it!...
finals = finals + item + " ";
System.out.print( item + " " );
}
t.setClickable(true);
t.setPaintFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
t.setMovementMethod(LinkMovementMethod.getInstance());
t.setText(Html.fromHtml(finals));
}
public void setPhoto(ImageView img, UrlImageViewHelper uHelper, AQuery aq, JSONObject json, int id){
String URL = "";
try {
URL = json.getJSONArray("images").getJSONObject(1).getString("source");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//uHelper.setUrlDrawable(img, URL);
// File file = aq.getCachedFile(URL);
// Bitmap bp = BitmapFactory.decodeFile(file.getPath());
// if (bp != null){
//
// img.setImageBitmap(bp);
//
// }else{
aq.id(img).image(URL, memCache, fileCache, 0, 0, null, 0, AQuery.RATIO_PRESERVE);
//
// }
}
public void getUrlPhoto(final ImageView img, final UrlImageViewHelper uHelper, String id, final AQuery aq, final int id_photo){
aq.ajax(BASE_URL + id, JSONObject.class, new AjaxCallback<JSONObject>() {
#Override
public void callback(String url, JSONObject json, AjaxStatus status) {
Log.d("url", url);
if(json != null){
setPhoto( img, uHelper, aq, json, id_photo);
}else{
Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
}
}
});
}
}