我使用asyncTask从服务器中提取图像并在imageViews上绘制它们。问题是图像有时会被“洗牌”,并且需要几秒钟来部署正确的图像。而且它也会变得迟钝。有没有更简单的方法呢?我做错了吗?
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(RecipeActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting to server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
//JSONObject jsono = new JSONObject(data);
//JSONArray array = jsono.getJSONArray("recepies");
JSONArray array = new JSONArray(data);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
Recepies rec = new Recepies();
rec.setName(object.getString("name"));
rec.setPciture(object.getString("picture"));
receptis.add(rec);
}
return true;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
adapter.notifyDataSetChanged();
if (result == false)
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
这是MainActivity:
ArrayList<Recepies> recepies;
String nameRecepie, ingredients, priprema, picture;
Toolbar toolbar;
RecipesAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipes_activity);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("Dog recepies");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
receptis = new ArrayList<Recepti>();
new JSONAsyncTask().execute("url");
ListView listview = (ListView) findViewById(R.id.list);
adapter = new RecipesAdapter(getApplicationContext(), R.layout.row, receptis);
listview.setAdapter(adapter); //onclick listener ->
和我的Adapter类
public class RecipesAdapter extends ArrayAdapter<Recepti> {
ArrayList<Recepti> receptisList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
public RecipesAdapter(Context context, int resource, ArrayList<Recepti> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
receptisList = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.imageview = (ImageView) v.findViewById(R.id.ivImage);
holder.tvName = (TextView) v.findViewById(R.id.tvName);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
new DownloadImageTask(holder.imageview).execute(receptisList.get(position).getSlika());
holder.tvName.setText(receptisList.get(position).getNaziv());
return v;
}
static class ViewHolder {
public TextView tvName;
public ImageView imageview;
}
protected class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}