I got this app that records the user’s Facebook ID in a MySQL table, and now I’m trying to get this ID and show the user’s profile picture on a ListView, but I don’t know what I’m doing wrong.
My custom ArrayAdapter:
public class VisualAdapter extends ArrayAdapter<String> {
private final Context contexto;
private final String[] comics, username, userid;
URL img_url = null;
Bitmap bm;
public VisualAdapter(Context contexto, String[] comics, String[] username,
String[] userid) {
super(contexto, R.layout.exibeimg, comics);
this.contexto = contexto;
this.comics = comics;
this.username = username;
this.userid = userid;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) contexto
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.exibeimg, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.tvTitleImage);
ImageView imageView = (ImageView) rowView.findViewById(R.id.ivImageVisual);
ImageView ivUser = (ImageView) rowView.findViewById(R.id.ivUser);
TextView tvUser = (TextView) rowView.findViewById(R.id.tvUserNAME);
textView.setText(comics[position]);
tvUser.setText(username[position]);
try {
img_url = new URL("http://graph.facebook.com/"+userid+"/picture?type=thumbnail");
bm = BitmapFactory.decodeStream(img_url.openConnection().getInputStream());
ivUser.setImageBitmap(bm);
}
catch (Exception e) {
e.printStackTrace();
}
return rowView;
}
}
Logcat: http://pastebin.com/kmLXKvZJ
Also, this is inside a tab. It’s in Tab 2, and when the app starts at Tab 1 when I click in Tab 2 (to see this list), it takes a few seconds to load, is there something that I can do to load it faster?
It is taking a few seconds to load because you are performing a network operation synchronously inside
getView(), which blocks the main UI thread from rendering the view every single time when it is waiting for a network response. This is very bad and what you need to do is to do an asynchronous lazy loading of images so that downloading the pictures is not performed on the UI thread.Read this great article on how to implement
ListViewcorrectly so that it does not take a few seconds to load.