我正在从Web服务加载数据并在ListView中显示它。 ListView项目只有两个视图 - 项目描述的文本和项目图像的图像。数据被下载并传递到AsyncTask中的自定义适配器,该适配器在onCreate方法中执行。除了ImageViews是空的,一切正常。
为了下载图像,我使用在网络上找到的ImageLoader类。它在我下载的示例中工作正常,它只需要两个参数 - 图像网址和图像视图,所以我认为一切都在这里。
我在这里做了一些根本错误的事情吗?
// Fetch latest items for home screen based on selected city
private class LatestItems extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... loc_id) {
// For storing data from web service
String data = "";
// Downloader object
UrlDownloader downloader = new UrlDownloader();
// Building the url to the web service
String url = "http://www.mywebsite.com/web_services/home_screen_latest_items.php?loc_id="
+ loc_id[0];
try {
// Fetching the data from web service in background
data = downloader.downloadUrl(url);
} catch (Exception e) {
Log.d("Downloader exception", e.toString());
}
return data;
}
@Override
protected void onPostExecute(String downloadedData) {
super.onPostExecute(downloadedData);
//If web service returned data
if (downloadedData != null) {
JSONObject jObject;
List<HashMap<String, String>> items = null;
// Instantiate parser
ItemJSONParser placeJsonParser = new ItemJSONParser();
try {
// Put data into JSON Object and pass it to parser
jObject = new JSONObject(downloadedData);
items = placeJsonParser.parse(jObject);
} catch (Exception e) {
Log.d("Exception", e.toString());
}
// Instantiate adapter. Provide Context, Item layout and
// DataSource (result)
ItemPreviewAdapter itemPreview = new ItemPreviewAdapter(
getApplicationContext(), R.layout.lvitem_homepage_item,
items);
// Pass each data to from list into to adapter
for (HashMap<String, String> data : items) {
itemPreview.add(data);
}
// Set adapter
latestItemsList.setAdapter(itemPreview);
}
}
}
项目布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/ivItemImage"
android:layout_width="75dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tvItemName"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center_horizontal"
android:lines="2"
android:textColor="#000"
android:textSize="10sp" />
</LinearLayout>
自定义适配器:
public class ItemPreviewAdapter extends ArrayAdapter<HashMap<String, String>> {
private final List<HashMap<String, String>> items;
private final LayoutInflater mLayoutInflater;
static class ViewHolder {
TextView tvIname;
ImageView image;
}
public ItemPreviewAdapter(final Context context,
final int textViewResourceId, List<HashMap<String, String>> map) {
super(context, textViewResourceId);
mLayoutInflater = LayoutInflater.from(context);
this.items = map;
Log.i("Adapter", items.toString());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(
R.layout.lvitem_homepage_item, parent, false);
holder = new ViewHolder();
holder.tvIname = (TextView) convertView
.findViewById(R.id.tvItemName);
holder.image = (ImageView) convertView
.findViewById(R.id.ivItemImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> item = items.get(position);
String id = item.get("id");
String item_name = item.get("iname");
holder.tvIname.setText(item_name);
String image_url = "http://www.mywebsite.com/images/items/cropped_image_list_id_"
+ id + ".png";
ImageLoader imgLoader = new ImageLoader(getContext());
imgLoader.DisplayImage(image_url, holder.image);
return convertView;
}
}
答案 0 :(得分:3)
将您的图像加载器类声明为全局并在Constructor中初始化。
ImageLoader imgLoader;
public ItemPreviewAdapter(final Context context,
final int textViewResourceId, List<HashMap<String, String>> map) {
super(context, textViewResourceId);
.....
.....
imgLoader = new ImageLoader(context);
}
还要从getView中删除它 真是太棒了......
答案 1 :(得分:0)
请使用活动的上下文,而不是使用Adapter类的getContext()
Context context;
public ItemPreviewAdapter(final Context context,final int textViewResourceId, List<HashMap<String, String>> map)
{
super(context, textViewResourceId);
mLayoutInflater = LayoutInflater.from(context);
this.items = map;
this.context=context;
Log.i("Adapter", items.toString());
}
并使用此上下文在getView方法
中初始化ImageLoader类ImageLoader imgLoader = new ImageLoader(context);