如何設置這種佈局的圖像?Android:如何在此ListView中設置圖像?
的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;
}
}
LoadThumbnail的AsyncTask:
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);
}
}
你能告訴我我需要適應哪些功能來實現這個功能嗎? 謝謝!
PS我已經研究瞭如何設置圖像和我能做到這一點,如果我已經有我所有的位圖,但我不知道如何與後臺下載 – Edd
所以,有一件事,你需要設置做到這一點圖像視圖(可能基於本地圖像路徑),如果圖像未找到,則將其保留爲佔位符,否則將圖像設置爲imageview。在AsyncTask的每個週期(即下載圖像)之後,只需調用Adapter.notifyDataSetChanged()。希望這可以幫助。 –
你應該使用imageDownloader類........... – Piyush