好的,我正在从服务器获取图像并在带有标题的gridview中显示它们...我使用baseadapter for gridview ...一切运行良好...我只有一个问题,我需要在获取数据时显示进度对话框来自服务器并填充在gridview上......我现在使用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;
}
这是我使用的异步任务:
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;
}
}
};
请告诉我我在哪里做错了什么......