Android在ListView异步线程获取网络图片

zqchn 2013-04-12 09:43:11
根据目前服务器的设置,我需要先发一些信息,去获取图片的名称。然后再发信息到服务器,获取各个图片,我目前所有图片只会在最后一行循环刷新。。。这几天也试了很多方法,一直不成功。求指教了。


public class FileListAdapter extends BaseAdapter {
private Context context; // 运行上下文
private List<Files> groupList = new ArrayList<Files>(); // 所有文件及文件夹
// private List<List<String>> childList = new
// ArrayList<List<String>>();//文件集合
private LayoutInflater listContainer; // 视图容器
private Files files;
private boolean isOk;
private boolean isFirst = true;
private Handler handler;
private ImageView imgfile;
private ListItemView listItemView;

public final class ListItemView { // 自定义控件集合
public Button downButton, friendButton, shareButton, renameButton;
public Button deleteButton;
private Button select_img;
public ImageView img_isView;
public TextView fileName, fileDate;
}

public FileListAdapter(Context context, List<Files> groups) {
this.context = context;
this.groupList = groups;
listContainer = LayoutInflater.from(context);
listItemView = new ListItemView();
handler = new Handler() {

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {

case 2:
notifyDataSetChanged();
break;

default:
break;
}
super.handleMessage(msg);
}

};

}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

if (convertView == null) {
convertView = listContainer.inflate(R.layout.item_filelist, null);
}
imgfile = (ImageView) convertView.findViewById(R.id.img_fileimg);
// 初始化绑定控件
listItemView.deleteButton = (Button) convertView
.findViewById(R.id.btn_delete);
listItemView.downButton = (Button) convertView
.findViewById(R.id.btn_download);
listItemView.friendButton = (Button) convertView
.findViewById(R.id.btn_friend);
listItemView.renameButton = (Button) convertView
.findViewById(R.id.btn_rename);
listItemView.shareButton = (Button) convertView
.findViewById(R.id.btn_share);
listItemView.fileName = (TextView) convertView
.findViewById(R.id.txt_fileName);
listItemView.fileDate = (TextView) convertView
.findViewById(R.id.txt_fileDate);
listItemView.select_img = (Button) convertView
.findViewById(R.id.btn_select);

listItemView.img_isView = (ImageView) convertView
.findViewById(R.id.img_checkimg);

// 进行初始化赋值
initData(convertView, position);
setListener(convertView, position, parent);

return convertView;
}

private void setListener(final View convertView, final int position,
final ViewGroup parent) {
// TODO Auto-generated method stub
final Files Cur_file = groupList.get(position);
final String Cur_fileName = groupList.get(position).getFileName();


}

private void initData(View convertView, final int position) {
// TODO Auto-generated method stub
Files files = new Files();
files = groupList.get(position);
listItemView.fileName.setText(files.getFileName());

// 判断是文件
if (files.isFile()) {
String fileType = files.getFileName().substring(
files.getFileName().lastIndexOf("."),
files.getFileName().length());
listItemView.fileDate.setText(files.getLastModified() + " "
+ files.getLength());
listItemView.downButton.setText("下载");
listItemView.downButton.setTag("下载");
if (fileType.equals(".jpg") || fileType.equals(".png")
|| fileType.equals(".gif") || fileType.equals(".Jpeg")
|| fileType.equals(".bmp")) {
//我现在就是要在这里,如果是图片。我就要去服务器获取文件名,文件名的获取是通过
//HttpCookieUtils httpCookieUtils = new HttpCookieUtils();
//httpCookieUtils.preview( groupList.get(position).getPath(),
// "40", "40");
//这个返回的是一个字符串,就是文件名。然后再设置缩略图,但是一直不成功,只会在最后一行循环刷新图片。而且线程一直在跑
//下面注释的都是我之前写的代码

/*
* new Thread() {
*
* @Override public void run() { // TODO Auto-generated method
* stub HttpCookieUtils httpCookieUtils = new HttpCookieUtils();
* String msg = null; Bitmap bitmap = null; try { msg =
* httpCookieUtils.preview( groupList.get(position).getPath(),
* "40", "40"); bitmap = BitmapUtils
* .getHttpBitmap(Constants.API_HOST + Constants.API_SHOW +
* msg); } catch (NotLoginException e) { // TODO Auto-generated
* catch block e.printStackTrace(); } catch (IOException e) { //
* TODO Auto-generated catch block e.printStackTrace(); }
* Message m = handler.obtainMessage(1, 1, 1, bitmap);
* handler.sendMessage(m); super.run(); }
*
*
* }.start();
*/

}

} else {
listItemView.fileDate.setText(files.getLastModified());
listItemView.downButton.setText("打开");
listItemView.downButton.setTag("打开");
groupList.get(position).setFileImg("file");
imgfile.setBackgroundResource(R.drawable.img_filelist);
}
}

...全文
283 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunmable 2013-04-12
  • 打赏
  • 举报
回复
试一下imagecache开源包
veardn 2013-04-12
  • 打赏
  • 举报
回复
这个网上应该一搜一大堆把,很多完整的demo都有的下载
zqchn 2013-04-12
  • 打赏
  • 举报
回复
ok,解决了。。。

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧