0

I want to create a list view with different images and text on each row.I created array for text and images but i am not getting how to do it further?

user594720
  • 449
  • 3
  • 9
  • 18
  • May be this link helps you. http://www.androidguys.com/2008/07/14/fancy-listviews-part-one/ – Mudassir Mar 08 '11 at 11:46
  • You should try to use the search feature on this site before asking a new question. Here you can find a nice answer with all the source code provided: [Listview with images](http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012) – Adinia Mar 08 '11 at 11:55

1 Answers1

0
  ListView list=(ListView)findViewById(android.R.id.list);
  list.setAdapter(new EfficientAdapter(this));

  String[] alltext={..............};
  String[] alImages={..........};

private static class EfficientAdapter extends BaseAdapter 
{
   private LayoutInflater mInflater;

   public EfficientAdapter(Context context) 
   {
   mInflater = LayoutInflater.from(context);
   }

   public int getCount() 
   {
   return alltext.length;
   }

   public Object getItem(int position) 
   {
   return position;
   }

   public long getItemId(int position) 
   {
       return position;
   }

   public View getView(int position, View convertView, ViewGroup parent) 
   {
   ViewHolder holder;
   if (convertView == null) 
   {
   convertView = mInflater.inflate(R.layout.images, null);
   holder = new ViewHolder();

   holder.title = (TextView) convertView.findViewById(R.id.title);
   holder.image = (ImageView) convertView.findViewById(R.id.image);

   convertView.setTag(holder);
   } 
   else 
   {
   holder = (ViewHolder) convertView.getTag();
   }

   holder.title.setText(alltext[position]);    
   holder.image.setImageBitmap(loadImageFromUrl(allImges[position]));
   return convertView;
   }

   static class ViewHolder 
   {
   TextView title;
   ImageView image;


   }
   }

If you have image urls then use loadImageFromUrl() method or in drawble folder use this one

   holder.image.setImageResource(allImges[position]);

The loadImageFromUrl method is

public static  Bitmap loadImageFromUrl(String url) {
    InputStream inputStream;Bitmap b,result;

    try {
          if(url.contains(" ")){
                url=url.replace(" ", "%20");
                  }


            inputStream = (InputStream) new URL(url).getContent();
            BitmapFactory.Options bpo=  new BitmapFactory.Options();
            bpo.inJustDecodeBounds = true;
            bpo.inJustDecodeBounds = false; 

            if(bpo.outWidth>500){ 
            bpo.inSampleSize = 8; 
            b=BitmapFactory.decodeStream(inputStream, null,bpo );
    } 
    else
    {
        bpo.inSampleSize=2;
        b=BitmapFactory.decodeStream(inputStream, null,bpo );
    }
             return  b;
    } catch (IOException e) {
            throw new RuntimeException(e);
        }

}

The images.xml is

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="2sp">


  <TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="45px"
 />


  <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

<ImageView
android:id="@+id/image"
android:layout_width="35px"
android:layout_height="wrap_content"
android:layout_marginLeft="2px"/>


  </LinearLayout>
  </RelativeLayout>
Ramakrishna
  • 4,066
  • 16
  • 48
  • 72