我有为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);
但我在film_item中有一个ImageView,我还需要为ListView中的每个项目绑定drawable的不同图像。我该怎么做?谢谢。
答案 0 :(得分:1)
这是一个有效的例子
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>