如何在此布局中设置图像?
ListView 将包含上述 3 个条目,每个图像将在一个AsyncTask
(见下文)中下载,文本将由预设字符串的字符串数组填充,例如
String[] values = {"string one", "string two", "String three"};
我希望能够首先使用下面的适配器设置所有 3 个条目的字符串内容值,然后让 AsyncTasks 在后台运行,下载并设置每个条目的图标。
字符串比图标更重要,所以我不希望用户在设置字符串之前必须等待每个图标下载。
我有一个 ListView 布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="@dimen/small_8dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/small_8dp" >
<ImageView
android:id="@+id/logo"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/loading"
android:scaleType="fitXY"
android:src="@drawable/image" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/small_8dp"
android:text="@string/loading"
android:textSize="@dimen/medium_15dp" >
</TextView>
</LinearLayout>
在布局中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_white"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/list_headlines">
</ListView>
</LinearLayout>
我一直在使用这个自定义适配器:
private class ArticleAdapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;
public ArticleAdapter(Context context, String[] values) {
super(context, R.layout.list_entry, values);
this.context=context;
this.values=values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_entry,parent,false);
TextView tv = (TextView) rowView.findViewById(R.id.label);
tv.setText(values[position]);
return rowView;
}
}
加载缩略图异步任务:
protected class LoadThumbnail extends AsyncTask<String, Void, String> {
private String url;
private boolean loaded; //if loaded set the bitmap image to whats downloaded
private Bitmap icon;
private int iconIndex;
public LoadThumbnail(int iconIndex, String url){
loaded = false;
this.url = url; //url of the icon to download
this.iconIndex=iconIndex; //Which icon in the listview were downloading
}
@Override
protected String doInBackground(String... params) {
Download download = new Download(url); //My Download Class
try {
icon = download.downloadImage(); //Returns A Bitmap image
loaded=true; //If no errors caught
} catch (Exceptions e) {
//Various Exception Handling Here
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
你能告诉我我必须适应哪些功能才能实现这一目标吗?谢谢!