Wednesday, June 16, 2010

ListView with icon loaded from internet

With the last exercise "Load ImageView with bitmap from internet", we can now modify the the former exercise "Using convertView in getView() to make ListView efficient" to load the icon from internet, instead of loading from our resources.

ListView with icon loaded from internet

please note that in order to permit the App to access to internet, we have to grand it permission of "android.permission.INTERNET"; refer to the last exercise "Load ImageView with bitmap from internet"

Keep usng the /res/layout/row.xml as previous exercise, "ListView, with icon".
package com.exercise.AndroidList;


import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidList extends ListActivity {

String image_URL=

public class MyCustomAdapter extends ArrayAdapter<String> {

Bitmap bm;

public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub

BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
bm = LoadImage(image_URL, bmOptions);


public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);

View row = convertView;

LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);

TextView label=(TextView)row.findViewById(;
ImageView icon=(ImageView)row.findViewById(;


return row;

String[] month = {
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
/*setListAdapter(new ArrayAdapter<String>(this,
R.layout.row,, DayOfWeek));*/
setListAdapter(new MyCustomAdapter(AndroidList.this, R.layout.row, month));

protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
//super.onListItemClick(l, v, position, id);
String selection = l.getItemAtPosition(position).toString();
Toast.makeText(this, selection, Toast.LENGTH_LONG).show();

private Bitmap LoadImage(String URL, BitmapFactory.Options options)
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in, null, options);
} catch (IOException e1) {
return bitmap;

private InputStream OpenHttpConnection(String strURL) throws IOException{
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();

HttpURLConnection httpConn = (HttpURLConnection)conn;

if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
catch (Exception ex)
return inputStream;


next: Load ListView in background AsyncTask


hanumantha said...

Very Thanks for you.

thpoul said...

Is it possible to implement an asynchronous image loader to this?

Android Er said...

thpoul: Pls. refer to Load ListView in background AsyncTask

zoran said...

Can we use more icon loaded from internet? example: One icon for one row...

How Can I make connection, example:
When I touch "one row" in ListView I want to, open or connect, with new .xml in android app.
How can I make that??

Thanks in advance

Android Er said...

hello zorzn,

Please refer Custom ArrayAdapter, with different icons

zoran said...


I saw that tutorial and I don't know how I can make connections...

I would aprechiate if you could give me source or tutorial when I loaded example: 5 or 7 or 10 different icon from internet...

How can I load an text from internet in list view?

I made Toast...(onListitemClick) in Listview but I would like to make next exemple:
When I touch "one"row" in ListView
I want to load samo information from internet in new "window"

Thanks in advance

Android Er said...

hello zoran,

where is the source of your image and text? how you load it in your Toast version?

zoran said...


Sorce of image and text is on the internet... exemple, for image "" and for text or .doc

I would like to load 5,6,7 different image and different text from internet in listview and I would like, when I click on "one row" to open new session where I again want to pick up some information from internet (examlpe or doc)

Thanks in advance

Anonymous said...

thanks its really working good.