Java 如何在ListView中设置项目的图像?

Java 如何在ListView中设置项目的图像?,java,android,Java,Android,我有为ListView创建适配器的代码: ListView films=(ListView)findViewById(R.id.listViewCurrentFilms); ArrayList<HashMap<String, String>> list=getList(); String[] fields=new String[]{"title", "director", "cast"}; int[] resources=new int[]{R.id.textView

我有为ListView创建适配器的代码:

ListView films=(ListView)findViewById(R.id.listViewCurrentFilms);

ArrayList<HashMap<String, String>> list=getList();

String[] fields=new String[]{"title", "director", "cast"};
int[] resources=new int[]{R.id.textViewFilmName, R.id.textViewDirector, R.id.textViewStart};

SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.film_item, fields, resources);
films.setAdapter(adapter);
ListView电影=(ListView)findViewById(R.id.listViewCurrentFilms);
ArrayList=getList();
字符串[]字段=新字符串[]{“标题”、“导演”、“演员”};
int[]resources=newint[]{R.id.textViewFilmName,R.id.textViewDirector,R.id.textViewStart};
SimpleAdapter=新SimpleAdapter(此,列表,R.layout.film_项,字段,资源);
胶片。设置适配器(适配器);

但是我在film_项中有一个ImageView,并且我还需要为ListView中的每个项从drawable绑定不同的图像。我怎么做?多谢各位

这是一个有效的例子

import java.io.ByteArrayInputStream;
import java.util.List;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class StockQuoteAdapter extends ArrayAdapter {
     private final Activity activity;
        private final List stocks;

        public StockQuoteAdapter(Activity activity, List objects) {
            super(activity, R.layout.movie , objects);
            this.activity = activity;
            this.stocks = objects;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View rowView = convertView;
            StockQuoteView sqView = null;

            if(rowView == null)
            {
                // Get a new instance of the row layout view
                LayoutInflater inflater = activity.getLayoutInflater();
                rowView = inflater.inflate(R.layout.stock, null);

                // Hold the view objects in an object,
                // so they don't need to be re-fetched
                sqView = new StockQuoteView();
                sqView.ticker = (TextView) rowView.findViewById(R.id.ticker_symbol);
                sqView.quote = (TextView) rowView.findViewById(R.id.ticker_price);
                sqView.time = (TextView) rowView.findViewById(R.id.showtimelist);
                sqView.img = (ImageView) rowView.findViewById(R.id.Image);
                sqView.btn = (Button) rowView.findViewById(R.id.lmbtn);
                sqView.ll = (LinearLayout) rowView.findViewById(R.id.LinearLayout02);
                // Cache the view objects in the tag,
                // so they can be re-accessed later
                rowView.setTag(sqView);
            } else {
                sqView = (StockQuoteView) rowView.getTag();
            }

            // Transfer the stock data from the data object
            // to the view objects
            final StockQuote currentStock = (StockQuote) stocks.get(position);
            sqView.ticker.setText(currentStock.getTickerSymbol());
            sqView.quote.setText(currentStock.getT_name());
            sqView.time.setText(currentStock.getTime());

            try{
            byte[] bb = currentStock.getBb();
                ByteArrayInputStream imageStream = new ByteArrayInputStream(
                        bb);
                Bitmap theImage = BitmapFactory
                        .decodeStream(imageStream);
                Drawable d = new BitmapDrawable(theImage);
                sqView.img.setBackgroundDrawable(d);
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

            sqView.ll.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {


                }
            });

            return rowView;
        }

        protected static class StockQuoteView {
            protected TextView ticker;
            protected TextView quote;
            protected TextView time;
            protected ImageView img;
            protected Button btn;
            protected LinearLayout ll;
        }
}
添加到活动中

StockQuoteAdapter aa = new StockQuoteAdapter(this, stocks);
stock.xml是

<?xml version="1.0" encoding="utf-8" ?> 
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:padding="6dip" android:layout_height="match_parent" android:orientation="horizontal">
- <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
  <ImageView android:id="@+id/Image" android:layout_height="80px" android:layout_width="60px" android:layout_margin="15px" /> 
- <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">
  <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:id="@+id/ticker_symbol" android:textColor="#000000" android:textStyle="bold" /> 
  <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:id="@+id/ticker_price" android:textColor="#000000" /> 
  <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:id="@+id/showtimelist" android:textColor="#000000" /> 

  </LinearLayout>
  </LinearLayout>
  </LinearLayout>

- 
- 
- 
看一看