好吧我从服务器获取图像并在 gridview 中显示标题...我使用 baseadapter 进行gridview ...一切都运行正常...我只有一个问题,我需要在从服务器获取数据并填充在gridview上时显示 progressdialog ... 我现在正在使用 AsyncTask 来显示进度对话框但它冻结了几个(如10)秒然后gridview显示......
这是我的baseadapter类:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = listInflater.inflate(R.layout.grid_adapter_view, null);
holder = new ViewHolder();
holder.gridImg = (ImageView) convertView
.findViewById(R.id.grid_item_image);
holder.gridTitle = (TextView) convertView
.findViewById(R.id.grid_item_label);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.gridTitle.setText(catItems.get(position).getName());
try {
URL url = new URL(catItems.get(position).getImg());
Bitmap img = BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
holder.gridImg.setImageBitmap(img);
} catch (Exception e) {
// TODO Auto-generated catch block
holder.gridImg.setImageResource(R.drawable.no_image);
e.printStackTrace();
}
return convertView;
}
public class ViewHolder {
ImageView gridImg;
TextView gridTitle;
}
这里是asynctask im使用:
mytask = new AsyncTask<Void, Void, Boolean>() {
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
main_grid.setAdapter(adater);
if (progress.isShowing()) {
progress.dismiss();
}
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progress.show();
}
@Override
protected Boolean doInBackground(Void... params) {
JSONArray jArray = request.getCategories();
try {
for (int i = 0; i < jArray.length(); i++) {
CategoriesDetail catDetail = new CategoriesDetail();
JSONObject jobj = jArray.getJSONObject(i);
catDetail.setId(jobj.getString("id").toString());
catDetail.setName(jobj.getString("name").toString());
catDetail.setImg(jobj.getString("img").toString());
catDetail.setOrder(jobj.getString("order").toString());
items.add(catDetail);
}
adater = new CatGridAdapter(MainActivity.this, items);
return true;
} catch (Exception e) {
e.printStackTrace();
progress.dismiss();
Toast.makeText(MainActivity.this,
"Error: " + e.getMessage(), Toast.LENGTH_LONG)
.show();
return false;
}
}
};
请告诉我哪里以及我做错了什么......
答案 0 :(得分:1)
您正在UI线程上进行网络操作。
BitmapFactory.decodeStream(url.openConnection().getInputStream());
看看这个:http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
修改强>
不是在getView()
中解码流,而是仅将默认图像设置为gridImg
并触发loadBitmap(catItems.get(position).getImg(), holder.gridImg)
。
public void loadBitmap(String url, ImageView imageView) {
new BitmapWorkerTask(imageView).execute(url);
}
class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected Bitmap doInBackground(String... params) {
return BitmapFactory.decodeStream(params[0].openConnection().getInputStream());
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
这一切都在上面的链接中描述。你应该阅读它以更好地理解android中的线程。