问题描述
如何设置图像在此布局?
How do i set the images in this layout?
ListView控件将包含以上项目3个,每个图像将会在下载的的AsyncTask
(见下文)和文本将通过一个String数组填写preSET字符串如
The ListView will contain 3 of the above entries, each image will be downloaded in an AsyncTask
(See Below) and the text will be filled in by a String array of preset Strings eg
String[] values = {"string one", "string two", "String three"};
欲能够首先使用下面的适配器,则有AsyncTasks在后台下载运行和设置的图标的每个条目设置的所有3项的字符串的内容的值。
I want to be able to first set the String content values of all 3 entries using the adapter below, then have AsyncTasks running in the background downloading and setting the icons for each entry.
所以我不希望用户必须等待设置该字符串之前每个图标下载琴弦比图标更重要。
The Strings are more important than the icons so i dont want the user to have to wait for each icon to download before the string is set.
我有一个ListView布局:
I have a ListView Layout:
<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>
香港专业教育学院一直与这个自定义适配器:
Ive been working with this custom adapter:
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的:
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);
}
}
你能告诉我它的功能我必须适应实现这一目标?
谢谢!
Can you tell me which functions i have to adapt to achieve this?Thanks!
推荐答案
要做到这一点,最简单的方法是将您的图像下载到一些收藏
,然后在列表中更新时下载是通过调用 notifyDatasetChanged
在适配器
完成。
The easiest way to do this is to download your images to some Collection
and then for the list to update when the downloads are complete by calling notifyDatasetChanged
on the Adapter
.
在我们开始之前 - 我不知道你的的AsyncTask
的位置。如果它的一个单独的类,你需要使用接口
来建立呼叫回你从它开始的地方。如果它在一个内部类的 Adaper
你可以做到这一点localY坐标。
Before we get started - I don't know where your AsyncTask
is located. If its a separate class you need to use an Interface
to set up a call back to the place you're starting it from. If its an inner class within your Adaper
you can do this localy.
什么你打算做的是建立你的适配器
(1)检查,如果你想要的图像可用(2)如果没有下载图片,并添加它到集合(3)当图像被下载刷新列表(或当所有图像下载depening这个名单是多久)。
What you're going to do is set up your Adapter
to (1) check if the image you want is available (2) if not download the image and add it to a collection (3) when the image is downloaded refresh the list (or when all images are downloaded depening how long this list is).
这一切都发生在你设置特定列表项的内容
This all happens when you set the content for a specific list item
if(logo != null){
if(imageCollection.contains(key){
logo.setImageBitmap(imageCollection.get(key));
} else {
thumbnailDownloader.execute;
}
}
如果您是的AsyncTask
是witin您adapater一个内部类,然后在 onPostExecute
你会(1)加图像以这个集合(2)更新的ListView
使用 notifyDatasetChanged
。如果你的的AsyncTask
是它自己的类,你将图像从那里并添加到collction在回调和更新列表。
If you're AsyncTask
is an inner class witin your adapater then within onPostExecute
you will (1) add the image to this collection (2) update the ListView
using notifyDatasetChanged
. If your AsyncTask
is it's own class you would add the image to the collction in your callback and update the list from there as well.
对于集
更容易,如果你使用 LruCache
内置的Android,但你可以使用的HashMap
或别的东西。只是取决于您的实现。
For the collection
it's easier if you use the LruCache
built into android but you could use a HashMap
or something else. Just depending on your implementation.
这篇关于Android的:如何设置此ListView中的形象呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!