GetView method from ArrayAdapter getting called onScroll up and down - android

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

Related

Implementing Picasso not loading image

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.

ListView CustomAdapter getView is not calling

Below is my code, here i am getting response..
Fetching Data from server in josn
getRequest();
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
//
private void getRequest(){
Thread trd = new Thread(new Runnable() {
#Override
public void run() {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
//
try{
Log.d("Get top called","try block");
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
if(response!=null){
try{
HashMap<String, String> defaultSongsDetails;
songs = new JSONArray(response);
JSONObject jsonObject;
for(int i=0;i<songs.length();i++){
defaultSongsDetails = new HashMap<String, String>();
String title,artist,language,imageUrl,songUrl,vocalUrl,duration=null;
String sdCardPathOfImage,sdCardPathOfOriginalSong,sdCardPathOfVocalSong=null;
jsonObject = songs.getJSONObject(i);
title = jsonObject.getString(SONG_TITLE);
defaultSongsDetails.put("song_title",title);
artist = jsonObject.getString(SONG_ARTIST);
defaultSongsDetails.put("song_artist",artist);
language = jsonObject.getString(SONG_LANGUAGE);
defaultSongsDetails.put("song_language",language);
duration = jsonObject.getString(SONG_DURATION);
defaultSongsDetails.put("song_duration",duration);
imageUrl = jsonObject.getString(SONG_THUMBNAL);
songUrl = jsonObject.getString(SONG_DOWNLOAD_URL);
vocalUrl = jsonObject.getString(SONG_VOCAL_URL);
String thumbnail=null;
int cutImg = imageUrl.lastIndexOf('/');
if (cutImg != -1) {
thumbnail = imageUrl.substring(cutImg + 1);
}
sdCardPathOfImage = basepath + "/songs/" + thumbnail;
defaultSongsDetails.put("song_thumbnail", sdCardPathOfImage);
String fileName=null; int cut = songUrl.lastIndexOf('/'); if (cut != -1) { fileName = songUrl.substring(cut + 1); }
sdCardPathOfOriginalSong = basepath+"/songs/" + fileName;
defaultSongsDetails.put("song_original_path",
sdCardPathOfOriginalSong);
String vocalFileName=null; int cutVocal = songUrl.lastIndexOf('/'); if (cut != -1) { vocalFileName =
songUrl.substring(cutVocal + 1); } sdCardPathOfVocalSong =
basepath+"/songs/" + vocalFileName;
defaultSongsDetails.put("song_vocal_path", sdCardPathOfVocalSong);
//Adding Hashmap to ArrayList
defaultSongsDetailList.add(defaultSongsDetails);
adapter = new CustomList(JsonActivity.this,defaultSongsDetailList);
}catch (Exception e){ e.printStackTrace(); } } }catch (Exception e) { e.printStackTrace(); } }
}); trd.start();
}
//CustomList Adapter class
public class CustomList extends ArrayAdapter<HashMap<String, String>>{
private final Activity context;
private ArrayList<HashMap<String, String>> songList;
public CustomList(Activity context,
ArrayList<HashMap<String, String>> songList) {
super(context, R.layout.list_item_view, songList);
this.context = context;
this.songList = songList;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_item_view, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.title);
ImageView imageView = (ImageView) rowView.findViewById(R.id.list_image);
TextView txtArtist = (TextView) rowView.findViewById(R.id.artist);
TextView txtDuration = (TextView) rowView.findViewById(R.id.duration);
System.out.println(" title ** "+songList.get(position).get("song_title")+" duration **
"+songList.get(position).get("song_duration")
+" thumb ** "+songList.get(position).get("song_thumbnail")+" language **
"+songList.get(position).get("song_language"));
txtArtist.setText(songList.get(position).get("song_title"));
txtDuration.setText(songList.get(position).get("song_duration"));
File file = new File(songList.get(position).get("song_thumbnail"));
Uri uri = Uri.fromFile(file);
imageView.setImageBitmap(BitmapFactory.decodeFile(defaultFilePath));
imageView.setImageURI(uri);
txtTitle.setText(songList.get(position).get("song_language"));
return rowView;
}
}
Any help would be appreciated...
//Fetching Data from server in josn
getRequest();
gAdapter = new BaseAdapter(HomeJsonActivity.this, defaultSongsDetailList);;
list = (ListView) findViewById(R.id.list);
gAdapter.notifyDataSetChanged();
list.setAdapter(gAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
private void getRequest() {
Thread trd = new Thread(new Runnable() {
#Override
public void run() {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
try {
Log.d("Get top called", "try block");
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
if (response != null) {
try {
HashMap<String, String> defaultSongsDetails;
songs = new JSONArray(response);
JSONObject jsonObject;
defaultSongsDetails = new HashMap<String, String>();
String title, artist, language, imageUrl, songUrl, vocalUrl, duration = null;
String sdCardPathOfImage, sdCardPathOfOriginalSong, sdCardPathOfVocalSong = null;
jsonObject = songs.getJSONObject(i);
title = jsonObject.getString(SONG_TITLE);
defaultSongsDetails.put("song_title", title);
artist = jsonObject.getString(SONG_ARTIST);
defaultSongsDetails.put("song_artist", artist);
language = jsonObject.getString(SONG_LANGUAGE);
defaultSongsDetails.put("song_language", language);
duration = jsonObject.getString(SONG_DURATION);
defaultSongsDetails.put("song_duration", duration);
imageUrl = jsonObject.getString(SONG_THUMBNAL);
songUrl = jsonObject.getString(SONG_DOWNLOAD_URL);
vocalUrl = jsonObject.getString(SONG_VOCAL_URL);
String thumbnail = null;
int cutImg = imageUrl.lastIndexOf('/');
if (cutImg != -1) {
thumbnail = imageUrl.substring(cutImg + 1);
}
sdCardPathOfImage = basepath + "/songs/" + thumbnail;
defaultSongsDetails.put("song_thumbnail", sdCardPathOfImage);
String fileName = null;
int cut = songUrl.lastIndexOf('/');
if (cut != -1) {
fileName = songUrl.substring(cut + 1);
}
sdCardPathOfOriginalSong = basepath + "/songs/" + fileName;
defaultSongsDetails.put("song_original_path", sdCardPathOfOriginalSong);
String vocalFileName = null;
int cutVocal = songUrl.lastIndexOf('/');
if (cut != -1) {
vocalFileName = songUrl.substring(cutVocal + 1);
}
sdCardPathOfVocalSong = basepath + "/songs/" + vocalFileName;
defaultSongsDetails.put("song_vocal_path", sdCardPathOfVocalSong);
defaultSongsDetailList.add(defaultSongsDetails);
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
trd.start();
}
//Custom Base Adapter Class
public class BaseAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<HashMap<String, String>> songList;
public BaseAdapter(Context context, ArrayList<HashMap<String, String>> list) {
this.mInflater = LayoutInflater.from(context);
this.songList = list;
for (HashMap hh : songList) {
System.out.println("**** hh ***" + hh);
}
}
#Override
public int getCount() {
return songList.size();
}
#Override
public Object getItem(int position) {
return songList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
ViewHolder holder;
if (convertView == null) {
rowView = mInflater.inflate(R.layout.list_item_view, parent, false);
holder = new ViewHolder();
holder.title = (TextView) rowView.findViewById(R.id.title);
holder.thumb = (ImageView) rowView.findViewById(R.id.list_image);
holder.artist = (TextView) rowView.findViewById(R.id.artist);
holder.duration = (TextView) rowView.findViewById(R.id.duration);
rowView.setTag(holder);
} else {
rowView = convertView;
holder = (ViewHolder) rowView.getTag();
}
holder.title.setText(songList.get(position).get("song_title"));
File file = new File(songList.get(position).get("song_thumbnail"));
Uri uri = Uri.fromFile(file);
holder.thumb.setImageURI(uri);
holder.artist.setText(songList.get(position).get("song_artist"));
holder.duration.setText(songList.get(position).get("song_duration"));
System.out.println(" title ** " + songList.get(position).get("song_title") + " duration ** " + songList.get(position).get("song_duration")
+ " thumb ** " + songList.get(position).get("song_thumbnail") + " language ** " + songList.get(position).get("song_language"));
return rowView;
}
private class ViewHolder {
public ImageView thumb;
public TextView title, duration, artist;
}
}

Image in listview ordering is not correct and download again

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.

How to make a chat app like Facebook?

I'm working on a chat app. I want to create a cool design for my listview. My code is working but I want to add some design similar to the design of Facebook, but I have a problem.
I can't do this: i.e one user has an ID=52 and the other has=5293. If the user has Id=52, the textarea gravity in the left and the other in the right, and here my code it doesn't see my if statement every time print the else statement I don't know why really I put my ID in an array but really this is the same result.
public void LoadMessage() {
// TODO Auto-generated method stub
DatabseHandler d = new DatabseHandler(EchangingMessage.this);
String me = d.getData();
TextView you_id = (TextView) findViewById(R.id.you_id);
String you = you_id.getText().toString().trim();
Log.i("ID :", me + " : " + you);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("me", me));
params.add(new BasicNameValuePair("you", you));
JSONObject json = jParser.makeHttpRequest(url_daftar_rs, "POST",
params);
try {
int success = json.getInt(TAG_SUCCESS);
Log.i("success :", "" + success);
if (success == 1) {
daftar_rs = json.getJSONArray(TAG_DAFTAR_RS);
for (int i = 0; i < daftar_rs.length(); i++) {
JSONObject c = daftar_rs.getJSONObject(i);
String id_rs = c.getString(TAG_ID_RS);
String nama_rs = c.getString(TAG_NAMA_RS);
String link_image_rs = "http://10.0.2.2/www/Android_Login_Secure/Images/upload/big/"
+ c.getString(TAG_LINK_IMAGE_RS);
String message_rs = c.getString(TAG_MESSAGE_RS);
String time_rs = c.getString(TAG_TIME_RS);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID_RS, id_rs);
map.put(TAG_NAMA_RS, nama_rs);
map.put(TAG_LINK_IMAGE_RS, link_image_rs);
map.put(TAG_MESSAGE_RS, message_rs);
map.put(TAG_TIME_RS, time_rs);
DaftarRS.add(map);
}
} else {
finish();
}
} catch (Exception e) {
Log.e("Error", "COnnection:" + e.toString());
}
runOnUiThread(new Runnable() {
public void run() {
// updating listview
SetListViewAdapter(DaftarRS);
}
});
}
public class ListAdapterSendMessage extends BaseAdapter {
public String POST_TEXT;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public final static String you_id = null;
int count = 0;
public ListAdapterSendMessage(Activity a,
ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.item_list_send, null);
}
HashMap<String, String> daftar_rs = new HashMap<String, String>();
daftar_rs = data.get(position);
String me = daftar_rs.get(MyMessages.TAG_LINK_IMAGE_RS);
if (me == "52") {
vi = inflater.inflate(R.layout.list_row_even, null);
TextView name_rs = (TextView) vi.findViewById(R.id.senderMessage);
TextView Des_rs = (TextView) vi.findViewById(R.id.message);
ImageView thumb_image = (ImageView) vi
.findViewById(R.id.imageSender);
TextView Time_rs = (TextView) vi.findViewById(R.id.senderTime);
name_rs.setText(daftar_rs.get(MyMessages.TAG_NAMA_RS));
// link_image_rs.setText(daftar_rs.get(MainActivity.TAG_LINK_IMAGE_RS));
// alamat_rs.setText(daftar_rs.get(MainActivity.TAG_ALAMAT_RS));
Des_rs.setText(daftar_rs.get(MyMessages.TAG_MESSAGE_RS));
Time_rs.setText(daftar_rs.get(MyMessages.TAG_TIME_RS));
imageLoader.DisplayImage(
daftar_rs.get(MyMessages.TAG_LINK_IMAGE_RS), thumb_image);
} else {
vi = inflater.inflate(R.layout.list_row_odd, null);
TextView name_rs = (TextView) vi.findViewById(R.id.senderMessage);
TextView Des_rs = (TextView) vi.findViewById(R.id.message);
ImageView thumb_image = (ImageView) vi
.findViewById(R.id.imageSender);
TextView Time_rs = (TextView) vi.findViewById(R.id.senderTime);
name_rs.setText(daftar_rs.get(MyMessages.TAG_NAMA_RS));
// link_image_rs.setText(daftar_rs.get(MainActivity.TAG_LINK_IMAGE_RS));
// alamat_rs.setText(daftar_rs.get(MainActivity.TAG_ALAMAT_RS));
Des_rs.setText(daftar_rs.get(MyMessages.TAG_MESSAGE_RS));
Time_rs.setText(daftar_rs.get(MyMessages.TAG_TIME_RS));
imageLoader.DisplayImage(
daftar_rs.get(MyMessages.TAG_LINK_IMAGE_RS), thumb_image);
}
return vi;
}
}
for this question we assume that my id =1 and you id =2
if (id==1) {
inflater.inflate(R.layout.item_list_right, parent, false);
else
{
inflater.inflate(R.layout.item_list_left, parent, false);
}
here you have to different layout one of them item_list_right and the other one is item_list_left
I solved this problem like this

i have one error passing of constructor as this in listfragments

I am newer to the fragments. In oncreate method i pass this value to Appetizerlist. but it shows an error. How to clear the error? Please help me.
public class MyListFragment1 extends ListFragment {
ImageView back;
String url = Main.url;
String Qrimage;
Bitmap bmp;
ListView list;
AppetiserFragment adapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.applistviewfragment, null);
list = (ListView) view.findViewById(R.id.list);
return view;
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
InputStream is = null;
String result = "";
JSONObject jArray = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url + "test.php3");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
// TODO: handle exception
Log.e("Log", "Error in Connection" + e.toString());
// Intent intent = new Intent(ViewQRCode.this, PimCarder.class);
// startActivity(intent);
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
jArray = new JSONObject(result);
JSONArray json = jArray.getJSONArray("appetiser");
adapter = new AppetiserFragment(this, json);
list.setAdapter(adapter);
} catch (Exception e) {
// TODO: handle exception
Log.e("log", "Error in Passing data" + e.toString());
}
}
}
AppetiserFragment.java
public class AppetiserFragment extends BaseAdapter {
String url = Main.url;
public Context Context;
String qrimage;
Bitmap bmp, resizedbitmap;
Bitmap[] bmps;
Activity activity = null;
private LayoutInflater inflater;
private ImageView[] mImages;
String[] itemimage;
TextView[] tv;
String itemname, price, desc, itemno;
String[] itemnames, checkeditems, itemnos;
String[] prices;
String[] descs;
HashMap<String, String> map = new HashMap<String, String>();
public AppetiserFragment(Context context, JSONArray imageArrayJson) {
Context = context;
// inflater =
System.out.println(imageArrayJson);
// (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// imageLoader=new ImageLoader(activity);
inflater = LayoutInflater.from(context);
this.mImages = new ImageView[imageArrayJson.length()];
this.bmps = new Bitmap[imageArrayJson.length()];
this.itemnames = new String[imageArrayJson.length()];
this.prices = new String[imageArrayJson.length()];
this.descs = new String[imageArrayJson.length()];
this.itemnos = new String[imageArrayJson.length()];
try {
for (int i = 0; i < imageArrayJson.length(); i++) {
JSONObject image = imageArrayJson.getJSONObject(i);
qrimage = image.getString("itemimage");
itemname = image.getString("itemname");
itemno = new Integer(i + 1).toString();
price = image.getString("price");
desc = image.getString("itemdesc");
System.out.println(price);
itemnames[i] = itemname;
prices[i] = price;
descs[i] = desc;
itemnos[i] = itemno;
byte[] qrimageBytes = Base64.decode(qrimage.getBytes());
bmp = BitmapFactory.decodeByteArray(qrimageBytes, 0,
qrimageBytes.length);
int width = 100;
int height = 100;
resizedbitmap = Bitmap.createScaledBitmap(bmp, width, height,
true);
bmps[i] = bmp;
mImages[i] = new ImageView(context);
mImages[i].setImageBitmap(resizedbitmap);
mImages[i].setScaleType(ImageView.ScaleType.FIT_START);
// tv[i].setText(itemname);
}
System.out.println(map);
} catch (Exception e) {
// TODO: handle exception
}
}
public AppetiserFragment() {
// TODO Auto-generated constructor stub
}
public int getCount() {
return mImages.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder viewHolder;
if (view == null) {
view = inflater.inflate(R.layout.appetiserlistview, null);
System.out.println("prakash");
viewHolder = new ViewHolder();
viewHolder.image = (ImageView) view
.findViewById(R.id.appetiserimage);
viewHolder.text = (TextView) view.findViewById(R.id.appetisertext);
viewHolder.desc = (TextView) view.findViewById(R.id.appetiserdesc);
viewHolder.price = (TextView) view
.findViewById(R.id.appetiserprice);
viewHolder.appitemnum = (TextView) view
.findViewById(R.id.appitemno);
// viewHolder.checkbox = (CheckBox) view.findViewById(R.id.bcheck);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.image.setImageBitmap(bmps[position]);
viewHolder.appitemnum.setText(itemnos[position]);
viewHolder.price.setText(prices[position]);
viewHolder.desc.setText(descs[position]);
// viewHolder.checkbox.setTag(itemnames[position]);
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(itemnames[position]);
return view;
}
static class ViewHolder {
protected TextView text, price, desc, appitemnum;
protected ImageView image;
public static CheckBox checkbox = null;
}
}
i Given whole code i want custom listview using listfragments
In the above code in this line, I'm getting an error at adapter = new Appetizerlist(this, json); Please tell me how to solve the problem. Help me.
as onCreate called before onCreateView so list will null there in onCreate ...........
http://developer.android.com/guide/topics/fundamentals/fragments.html
in oncreate
list.setAdapter(adapter);
in onCreateView you initlized that
list = (ListView) view.findViewById(R.id.list);
......
so move this line list.setAdapter(adapter); in onCreateView
You have error in following line.
adapter = new Appetizerlist(this, json);
Change it to
adapter = new Appetizerlist(getActivity().getApplicationContext(), json);

Categories

Resources