gpt4 book ai didi

Android:如何在此ListView中设置图像?

转载 作者:行者123 更新时间:2023-11-29 16:05:56 25 4
gpt4 key购买 nike

如何在此布局中设置图像?

List Entry

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);
}


}

你能告诉我我必须适应哪些功能才能实现这一目标吗?谢谢!

最佳答案

最简单的方法是将图像下载到一些 Collection然后在下载完成后通过调用 notifyDatasetChanged 更新列表在 Adapter 上.

开始之前 - 我不知道你的 AsyncTask 在哪里位于。如果它是一个单独的类,您需要使用 Interface设置回拨到您开始的地方。如果它是你的内部类 Adaper您可以在本地执行此操作。

您要做的是设置您的 Adapter (1) 检查您想要的图像是否可用 (2) 如果不可用,请下载图像并将其添加到集合中 (3) 下载图像时刷新列表(或下载所有图像时,取决于此列表的长度) ).

当您为特定列表项设置内容时,这一切都会发生

if(logo != null){
if(imageCollection.contains(key){
logo.setImageBitmap(imageCollection.get(key));
} else {
thumbnailDownloader.execute;
}
}

如果你是 AsyncTask是内部类,然后在您的适配器中 onPostExecute您将 (1) 将图像添加到此集合 (2) 更新 ListView使用 notifyDatasetChanged .如果你的AsyncTask是它自己的类,您可以在回调中将图像添加到集合中,并从那里更新列表。

对于 collection如果使用 LruCache 会更容易内置于 android 中,但您可以使用 HashMap或者是其他东西。仅取决于您的实现。

关于Android:如何在此ListView中设置图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18466175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com