爲了處理別的東西比一個String數組顯示爲TextView的在ListView,你需要編寫自己的自定義適配器(因此Android知道如何顯示這些項目)
下面是一個例子:
你的活動:
public class MyActivity extends Activity{
private ArrayList<URL> MY_DATA;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Sets the View of the Activity
setContentView(R.layout.my_activity);
ListView myList = (ListView) findViewById(R.id.myList);
MyAdapter adapter = new MyAdapter(this, MY_DATA);
listView.setAdapter(adapter);
}
你活動的佈局(my_activity.xml這裏):
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myList"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
以及自定義適配器:
public class MyAdapter extends BaseAdapter{
private LayoutInflater inflater;
private ArrayList<URL> data;
public EventAdapter(Context context, ArrayList<URL> data){
// Caches the LayoutInflater for quicker use
this.inflater = LayoutInflater.from(context);
// Sets the events data
this.data= data;
}
public int getCount() {
return this.data.size();
}
public URL getItem(int position) throws IndexOutOfBoundsException{
return this.data.get(position);
}
public long getItemId(int position) throws IndexOutOfBoundsException{
if(position < getCount() && position >= 0){
return position;
}
}
public int getViewTypeCount(){
return 1;
}
public View getView(int position, View convertView, ViewGroup parent){
URL myUrl = getItem(position);
ViewHolder holder = new ViewHolder(); // Use a ViewHolder to save your ImageView, in order not to have to do an expensive findViewById for each iteration
// DO WHAT YOU WANT WITH YOUR URL (Start a new activity to download image?)
if(convertView == null){ // If the View is not cached
// Inflates the Common View from XML file
convertView = this.inflater.inflate(R.id.my_row_layout, null);
holder.myImageView = (ImageView)findViewById(R.id.myRowImageView);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.myImageView.setImageBitmap(MY_BITMAP_I_JUST_DOWNLOADED);
return convertView;
}
static class ViewHolder{
ImageView myImageView;
}
}
最後你行的佈局(my_row_layout.xml這裏):
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myRowImageView"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
試試我的樣品http://stackoverflow.com/questions/541966/android-如何做我懶得加載的圖像在列表視圖/ 3068012#3068012 – Fedor 2010-11-18 14:23:30
thanx fedor for ur reply但它太複雜瞭解 – 2010-11-19 06:02:37
恐怕它不能做的更容易。你可以重用我的ImageLoader類而不理解它裏面的所有東西。 – Fedor 2010-11-19 06:50:58