Android异步加载学习笔记之三:用AsyncTask实现ListView中的图片数据加载

         前面的笔记Android异步加载学习笔记之一:用AsyncTask加载服务器json数据和笔记 Android异步加载学习笔记之二:实现ListView中的图片数据从网络加载中有一点小错误,如果给大家带来不便,还请谅解,首先不正确是是获取json数据的url地址写错了,正确的是:http://www.imooc.com/api/teacher?type=4&num=30,这个网址来自慕客网,我主要是学习这个网站的视屏,然后写点笔记,因为看了视屏很多都记不住。还有个错误就在解析json时,bean.setImgUrl(jobj.getString("picSmal"));中的picSmal应该是picSmall,少写了个l。今天在继续学习的时候,才发现昨之前笔记的代码不小心写错了。然后今天的学习目的就是把之前ListView中的图片数据和AsyncTask异步加载出来。贴代码:

public class MainActivity extends ActionBarActivity {
private ListView mListView;
private String url = "http://www.imooc.com/api/teacher?type=4&num=30";
private DataAdapter mAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.main_lv);
new MyAsyncTask().execute(url);
}


/**
* 解析返回的json数据为List<DataBean>
* @description:
* @author ldm
* @date 2015-8-10 下午8:44:19
*/
private List<DataBean> getGsonData(String url) {
List<DataBean> list = new ArrayList<DataBean>();


try {
String jsonStr = readStream(new URL(url).openStream());
JSONObject jobj = null;
DataBean bean = null;
jobj = new JSONObject(jsonStr);
JSONArray jsonArray = jobj.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
jobj = jsonArray.getJSONObject(i);
bean = new DataBean();
bean.setImgUrl(jobj.getString("picSmall"));
bean.setTitle(jobj.getString("name"));
bean.setContent(jobj.getString("description"));
list.add(bean);
}
}
catch (Exception e) {
e.printStackTrace();
}
return list;
}


private String readStream(InputStream is) {
InputStreamReader isr = null;
String result = "";
try {
isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String line = "";
while ((line = br.readLine()) != null) {
result += line;
}
}
catch (Exception e) {
e.printStackTrace();
}
return result;


}


/**
* 异步任务:将url中的数据访问
* @description:
* @author ldm
* @date 2015-8-10 下午8:43:52
*/
class MyAsyncTask extends AsyncTask<String, Void, List<DataBean>> {


@Override
protected List<DataBean> doInBackground(String... params) {
// TODO Auto-generated method stub
return getGsonData(params[0]);
}


@Override
protected void onPostExecute(List<DataBean> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
mAdapter = new DataAdapter(MainActivity.this, result);
mListView.setAdapter(mAdapter);
}
}
}

适配器类:

public class DataAdapter extends BaseAdapter {
private Context mContext;
private List<DataBean> list;


public DataAdapter(Context mContext, List<DataBean> list) {
this.mContext = mContext;
this.list = list;
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}


@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}


@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}


@Override
public View getView(int arg0, View view, ViewGroup arg2) {
ViewHolder holder=null;
if(view==null){
holder=new ViewHolder();
view=LayoutInflater.from(mContext).inflate(R.layout.item_layout, null);
holder.iv=(ImageView) view.findViewById(R.id.item_iv);
holder.titleTv=(TextView) view.findViewById(R.id.item_title);
holder.contentTv=(TextView) view.findViewById(R.id.item_content);
view.setTag(holder);
}else{
holder=(ViewHolder) view.getTag();
}
holder.titleTv.setText(list.get(arg0).getTitle());
holder.contentTv.setText(list.get(arg0).getContent());
holder.iv.setTag(list.get(arg0).getImgUrl());//为ImageView设置tag
// new ImageLoader().loaderImageThread(holder.iv, list.get(arg0).getImgUrl());//用线程加载图片
new ImageLoader().loadImgByAsyncTask(holder.iv, list.get(arg0).getImgUrl());
return view;
}


class ViewHolder {
TextView titleTv;
TextView contentTv;
ImageView iv;
}
}

数据实体bean:

public class DataBean {
private String imgUrl;
private String title;
private String content;


public String getImgUrl() {
return imgUrl;
}


public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}


public String getTitle() {
return title;
}


public void setTitle(String title) {
this.title = title;
}


public String getContent() {
return content;
}


public void setContent(String content) {
this.content = content;
}


@Override
public String toString() {
return "DataBean [imgUrl=" + imgUrl + ", title=" + title + ", content=" + content + ", getImgUrl()=" + getImgUrl() + ", getTitle()=" + getTitle() + ", getContent()=" + getContent() + "]";
}


}

然后是图片加载类:

public class ImageLoader {


/**
*从url中获取到Bitmap
* @description:
* @author ldm
* @date 2015-8-11 下午1:55:12
*/
public Bitmap getBitmapByUrl(String urlStr) {
Bitmap bitmap = null;
InputStream is = null;
try {
URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
is = new BufferedInputStream(con.getInputStream());
bitmap = BitmapFactory.decodeStream(is);
con.disconnect();
return bitmap;
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
is.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
return null;
}


public void loadImgByAsyncTask(ImageView img, String url) {
new ImageAsyncTask(img, url).execute(url);
}


private class ImageAsyncTask extends AsyncTask<String, Void, Bitmap> {
private ImageView imageView;
private String mUrl;


public ImageAsyncTask(ImageView imageView, String mUrl) {
this.imageView = imageView;
this.mUrl = mUrl;
}


@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
return getBitmapByUrl(params[0]);
}


@Override
protected void onPostExecute(Bitmap result) {
if (imageView.getTag().equals(mUrl)) {
imageView.setImageBitmap(result);
}
}
}


}

今天的代码是经过我测试过的,没有问题才敢写出来的,请大家多指教,感谢视屏中老师的无私奉献!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值