如何创建不同的列表视图。 android中的图片和文字?

时间:2011-03-08 11:42:13

标签: android

我想在每一行创建一个包含不同图像和文本的列表视图。我为文本和图像创建了数组,但我还没有得到如何进一步做到这一点?

1 个答案:

答案 0 :(得分: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;


   }
   }

如果您有图片网址然后使用loadImageFromUrl()方法或在drawble文件夹中使用此文件

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

loadImageFromUrl方法是

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

}

images.xml是

  <?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>