Reputation: 5192
In my application i have an listview ..Each row contains an image and a textview in relative layout..Everything is done..But the listview is not scrolling that much smooth as native "Contact list".I think the problem lies in getview(),y because in this method i have a "HTTP" call and bitmap image loading..Is there anyway to do this..Please help me..thanks in advance..
My Getview method:
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
Bitmap bitmap = DownloadImage(
kickerimage[position] );
// View listView = convertView;
ViewHolder holder;
if (convertView == null)
{
//this should only ever run if you do not get a view back
LayoutInflater inflater = (LayoutInflater) contxt
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.homelistrow, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView
.findViewById(R.id.icon);
holder.image.setImageBitmap(bitmap);
holder.text = (TextView) convertView
.findViewById(R.id.name_label);
holder.text.setText(itemsarray[position]);
}
return convertView ;
}
private Bitmap DownloadImage(String URL)
{
// System.out.println("image inside="+URL);
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// System.out.println("image last");
return bitmap;
}
private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK)
{
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}
Upvotes: 0
Views: 1001
Reputation: 9217
From your code I see that your are downloading images inside the getView() function in the same thread. Use webImageView
to do this task instead of native ImageView
.
here is the sample
Upvotes: 2
Reputation: 7902
The reason why your ListView
is not scrolling smoothly is because you are fetching an image from an InputStream on the mainthread. This means, whenever you want to load an image, the main thread is being delayed and your ListView
.
The solution is to load your images on a different thread then the main thread. This is called lazy loading of images and is done by starting a new thread (mostly done with an AsyncTask
) to load your images.
Here are some interesting links you can visit for examples:
http://andytsui.wordpress.com/2012/05/10/tutorial-lazy-loading-list-views-in-android-binding-44/
http://thinkandroid.wordpress.com/2012/06/13/lazy-loading-images-from-urls-to-listviews/
https://github.com/commonsguy/cwac-endless
Upvotes: 1