故事发生在一个非常离奇诡异的夜晚,我还是按照以前的老样子进行着敲代码的良好享受,键盘的节奏已经让我忘却了一切烦躁和热浪的冲击,敲完后我欣喜若狂的运行起来,突然,我发现了一个重大的错误,程序出了bug
事件如下:
适配器: 适配器中加入了一个线程(应该是多个,没调用一次getview就开启一个asyncTask)进行读取网路图片,
虚拟机出现的效果让我大吃一惊,有的图片变化多次,最终稳定下来。
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import com.example.android_clientreqxml.R;
import com.example.android_clientreqxml.vo.companys;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class customAdapter extends BaseAdapter {
private List<companys> list;
private Context context;
public customAdapter(List<companys> list, Context context) {
super();
this.list = list;
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
holder hold = null;
if (convertView == null) {
hold = new holder();
convertView = LayoutInflater.from(context).inflate(
R.layout.list_item, null);
hold.imageView = (ImageView) convertView
.findViewById(R.id.compnys_icon);
hold.textView = (TextView) convertView
.findViewById(R.id.company_name);
convertView.setTag(hold);
}
hold = (holder) convertView.getTag();
hold.textView.setText(list.get(position).getName());
//调用showBitmap
showBitmap(hold.imageView, list.get(position).getIcon());
return convertView;
}
/**
*
* @param imageView contentView 中的icon
* @param HttpName contentview 中具体position对象的Icon的URL
* 看到注销中的两个 应该注意:android中所有访问网络的延时操作,必须用到开启新的线程去执行
* 当调用一个getview时我们就调用一次此方法,方法会新建AsyncTask以执行每个item中的Icon
* 而后 imageview 的显示图片会在AsyncTask中执行
*/
public static void showBitmap(ImageView imageView, String HttpName){
try {
//getview执行太快 而asynctask访问网络很慢
// 会造成图片变换的bug,这种方案治标不治本。很是烦躁
Thread.currentThread().sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MyAsyncTask task=new MyAsyncTask(imageView, HttpName);
task.execute(new String[]{});
}
class holder {
ImageView imageView;
TextView textView;
}
}
下面是asyncTask 读取URL得到图片
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.PublicKey;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
public class MyAsyncTask extends AsyncTask<String, Bitmap, String>{
private ImageView imageView;
private String httpName;
public static final String o="123123";
Bitmap bitmap,bitmaps;
public MyAsyncTask(ImageView imageView, String httpName) {
super();
this.imageView = imageView;
this.httpName = httpName;
}
/**
* doinbackGrund执行完后 我们拿到bitmap 用imageview显示图片
*/
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
imageView.setImageBitmap(bitmaps);
imageView=null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Bitmap... values) {
super.onProgressUpdate(values);
bitmaps=values[0];
}
/**
* excute后我们拿到icon的url去访问网络
* 得到流,而后得到bitmap
*/
@Override