//以下是自定义适配器的的代码...
public class ListView_Adapter extends BaseAdapter {
private Context context;
private List<SingleInfo> items;
private H h;
public ListView_Adapter(Context context, List<SingleInfo> items) {
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
final int i = arg0;
if (arg1 == null) {
h = new H();
arg1 = LayoutInflater.from(context)
.inflate(R.layout.layout_2, null);
h.img = (ImageView) arg1.findViewById(R.id.intenet_img);
h.textView = (TextView) arg1.findViewById(R.id.intent_textview);
h.bar = (ProgressBar) arg1.findViewById(R.id.intenet_progressBar);
h.imgbtn = (ImageView) arg1.findViewById(R.id.intenet_img_downbtn);
arg1.setTag(h);
} else {
h = (H) arg1.getTag();
}
Bitmap bitmap = BitmapFactory.decodeStream(GetInputStream
.getNetInputStream(Final.IMG + items.get(arg0).getSingleImg()));
h.img.setImageBitmap(bitmap);
h.textView.setText(items.get(arg0).getSingleName());
h.imgbtn.setImageResource(R.drawable.down);
h.imgbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 设置progressBar为可见
h.bar.setVisibility(View.VISIBLE);
// 获得视图
new Thread(new Runnable() {
@Override
public void run() {
// 频好网络地址,并制定下载文件到什么路径下
downFile(items.get(i).getSingleName(),
Final.SINGLE_DIR_PATH
+ items.get(i).getSingleName());
}
}).start();
}
});
return arg1;
}
/**
* 获取二进制文件
*
* @param path
* 网络路径
* @param dirPath
* 下载的本地存放路径
*/
public void downFile(String singleName, String dirPath) {
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
String newSingleName = URLEncoder.encode(singleName, "utf-8");
newSingleName = newSingleName.replaceAll("\\+", "%20");
URL url = new URL(Final.SINGLE + newSingleName);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
int length = conn.getContentLength();
int len = 0;
int hasReader = 0;
int progress = 0;
byte[] buf = new byte[1024];
in = new BufferedInputStream(conn.getInputStream());
out = new BufferedOutputStream(new FileOutputStream(
isPath(dirPath)));
while ((len = in.read(buf)) != -1) {
hasReader += len;
System.out.println("已下载:" + hasReader);
progress = (int) ((double) hasReader / (double) length * 100);
Message meg = new Message();
meg.arg1 = progress;
handler.sendMessage(meg);
out.write(buf);
out.flush();
}
} else {
System.out.println("失败");
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 创建存放路径路径
private File isPath(String path) {
File file = new File(path);
if (file.exists()) {
file.delete();
}
file.getParentFile().mkdirs();