package com.xushouwei.adapter;
import java.io.File;
import java.util.List;
import com.xushouwei.asyncload.R;
import com.xushouwei.bean.Contact;
import com.xushouwei.service.ContactService;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
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;
/**
* 自定义联系人适配器
*
* @author 徐守威
*
*/
public class ContactAdapter extends BaseAdapter {
private List<Contact> data;
private int listviewItem;
private File cache;
private LayoutInflater layoutInflater;
public ContactAdapter(Context context, List<Contact> data, int listviewItem, File cache) {
// TODO Auto-generated constructor stub
this.data = data;
this.listviewItem = listviewItem;
this.cache = cache;
// 通过上下文对象取得布局填充服务
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/**
* 得到数据的总数
*/
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
/***
* 根据数据的索引得到集合总所对应的数据
*/
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
/**
* 当listview每显示一个条目的时候就会调用该方法
*/
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView;
TextView textView;
if (convertView == null) {
// 得到条目的view对象
convertView = layoutInflater.inflate(listviewItem, null);
// 因为每次查找的时候都要耗时因此我们可以定义一个内部类进行包装
imageView = (ImageView) convertView.findViewById(R.id.imageView);
textView = (TextView) convertView.findViewById(R.id.textView);
// 保存对象
convertView.setTag(new DataWrapper(imageView, textView));
} else {
DataWrapper dataWrapper = (DataWrapper) convertView.getTag();
imageView = dataWrapper.imageView;
textView = dataWrapper.textView;
}
Contact contact = data.get(position);
textView.setText(contact.name);
// 果如在此处直接加在图片的话容易报无响应错误
// 因此呀,我们这里需要采用异步加载数据处理的方式
asyncImageLoad(imageView, contact.image);
return convertView;
}
private void asyncImageLoad(ImageView imageView, String path) {
// TODO Auto-generated method stub
AsynImageTask asynImageTask=new AsynImageTask(imageView);
asynImageTask.execute(path);
}
private final class AsynImageTask extends AsyncTask<String,Integer,Uri>
{
private ImageView imageView;
public AsynImageTask(ImageView imageView) {
// TODO Auto-generated constructor stub
this.imageView=imageView;
}
@Override
protected void onPostExecute(Uri result) {
// 从消息中取得uri
if (result != null && imageView != null) {
// 给imageview设置图片
imageView.setImageURI(result);
}
}
//该方法在子线程中执行
@Override
protected Uri doInBackground(String... params) {
// TODO Auto-generated method stub
try {
return ContactService.getImage(params[0], cache);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
/*
// 异步加载图片方法(核心中的核心)
private void asyncImageLoad(final ImageView imageView, final String path) {
// TODO Auto-generated method stub
// 在子线程中如果要想更新ui的话这里就要采用Handler技术
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {// 该方法运行在主线程
// TODO Auto-generated method stub
// 从消息中取得uri
Uri uri = (Uri) msg.obj;
if (uri != null && imageView != null) {
// 给imageview设置图片
imageView.setImageURI(uri);
}
}
};
// 如果在内部类中要访问变量需要将变量声明成final
// 这里采用匿名内部类的方式处理
Runnable runnable = new Runnable() {
public void run() {
// TODO Auto-generated method stub
// 调用业务类中的取得图片方法(改方法通过图片路径获取,并返回图片的URI)
try {
Uri uri = ContactService.getImage(path, cache);
handler.sendMessage(handler.obtainMessage(10, uri));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
// 定义一个线程并启动
new Thread(runnable).start();
}
*/
private final class DataWrapper {
public ImageView imageView;
public TextView textView;
public DataWrapper(ImageView imageView, TextView textView) {
this.imageView = imageView;
this.textView = textView;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Android中使用ListView数据异步加载与AsyncTask.zip (58个子文件)
test
list.xml 2KB
images
1.gif 25KB
8.gif 6KB
12.gif 25KB
11.gif 29KB
10.gif 28KB
18.jpg 14KB
7.gif 13KB
4.gif 9KB
3.gif 13KB
13.gif 22KB
5.gif 15KB
15.jpg 18KB
2.gif 6KB
6.gif 22KB
14.gif 9KB
17.jpg 10KB
16.jpg 23KB
9.gif 8KB
DataAsyncLoad
.project 849B
bin
DataAsyncLoad.apk 19KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
resources.ap_ 11KB
classes
com
xushouwei
bean
Contact.class 552B
adapter
ContactAdapter$DataWrapper.class 748B
ContactAdapter$AsynImageTask.class 2KB
ContactAdapter.class 3KB
tools
MD5.class 1KB
asyncload
R$string.class 442B
R$layout.class 446B
MainActivity.class 2KB
R$drawable.class 422B
R.class 566B
MainActivity$2.class 1KB
R$attr.class 355B
MainActivity$1.class 1KB
R$id.class 466B
service
ContactService.class 4KB
classes.dex 11KB
proguard.cfg 1KB
.settings
org.eclipse.jdt.core.prefs 177B
AndroidManifest.xml 972B
src
com
xushouwei
bean
Contact.java 279B
adapter
ContactAdapter.java 5KB
tools
MD5.java 775B
asyncload
MainActivity.java 2KB
service
ContactService.java 4KB
project.properties 360B
res
values
strings.xml 188B
layout
listview_item.xml 623B
main.xml 404B
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 3KB
.classpath 364B
gen
com
xushouwei
asyncload
R.java 917B
assets
共 58 条
- 1
资源评论
- lzpqq_csdn2017-09-20神马玩意...
徐守威
- 粉丝: 72
- 资源: 109
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功