




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】ListView相关使用,优化Day08
packagecom.example.ex_day_0611_day07_listview;importjava.io.IOException;importjava.io.InputStream;import.MalformedURLException;import.URL;import.URLConnection;importjava.security.PublicKey;importjava.util.HashMap;importjava.util.concurrent.Executor;importandroid.os.AsyncTask;importandroid.os.Bundle;importandroid.app.Activity;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.view.LayoutInflater;importandroid.view.Menu;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;importandroid.widget.Button;importandroid.widget.ImageView;importandroid.widget.ListView;importandroid.widget.ProgressBar;importandroid.widget.TextView;publicclassMainActivityextendsActivity{ HashMap<Integer,Bitmap>map=newHashMap<Integer,Bitmap>(); HashMap<Integer,Boolean>hashmap=newHashMap<Integer,Boolean>(); HashMap<Integer,MyAsyncTask2>myapa=newHashMap<Integer,MyAsyncTask2>(); HashMap<Integer,Integer>myint=newHashMap<Integer,Integer>(); privateListViewlistview; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview=(ListView)findViewById(R.id.listView1); MyApaptermyApapter=newMyApapter();
listview.setAdapter(myApapter); } classUser { ImageViewp_w_picpathview; ProgressBarpb; TextViewtv; Buttonbutton; }
classMyApapterextendsBaseAdapter
{ @Override
publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
Viewview=null;
Useruser=newUser(); LayoutInflaterinflater=getLayoutInflater(); if(convertView==null) {
view=inflater.inflate(R.layout.itmp,null);
user.p_w_picpathview=(ImageView)view.findViewById(R.id.p_w_picpathView1);
user.pb=(ProgressBar)view.findViewById(R.gressBar1);
user.button=(Button)view.findViewById(R.id.button1);
user.tv=(TextView)view.findViewById(R.id.textView1);
view.setTag(user); }else { view=convertView; user=(User)view.getTag(); } //下载图片 //设置图片还未开始下载时的状态 user.p_w_picpathview.setImageResource(R.drawable.ic_launcher); Bitmapbitmap=map.get(position); if(bitmap==null)
{ MyAsyncTaskmyp_w_picpathview=newMyAsyncTask(user.p_w_picpathview,position,user.pb); myp_w_picpathview.execute(":8080/service/qq"+position+".png"); }else { user.p_w_picpathview.setImageBitmap(bitmap); } //实现按钮点击事件 finalButtonbutton1=user.button; finalintpos=position; finalProgressBarpb1=user.pb; finalTextViewtv=user.tv; button1.setOnClickListener(newOnClickListener(){ @Override publicvoidonClick(Viewv){
button1.setText("下载中");
button1.setEnabled(false);
MyAsyncTask2myasyctask2=newMyAsyncTask2(button1,pb1,tv,pos);
myapa.put(pos,myasyctask2);
myasyctask2.execute(":8080/service/qq"+pos+".zip");
hashmap.put(pos,null); } }); MyAsyncTask2task2=myapa.get(pos); ProgressBarpb2=user.pb; Buttonbutton2=user.button; TextViewtv1=user.tv; //按钮点击事件中设置进度条。 //下文件 if(hashmap.get(position)==null) { button1.setText("下载"); button1.setEnabled(true); user.tv.setText("=========="); pb1.setProgress(0); } elseif(hashmap.get(position)==false) { button1.setText("下载中"); button1.setEnabled(false); user.pb.setMax(myint.get(pos));
task2.setReplace(pb2,button2,tv1);
}elseif(hashmap.get(position)==true) { button1.setText("下载完成"); button1.setEnabled(true); user.tv.setText("=========="); pb1.setMax(100); pb1.setProgress(100); } returnview;
} @Override publicintgetCount(){ return20; } @Override publicObjectgetItem(intposition){ returnnull; } @Override publiclonggetItemId(intposition){ return0; }
}
classMyAsyncTask2extendsAsyncTask<String,Integer,String>
{
Buttonbutton1;
ProgressBarpb;
TextViewtv;
intposition; publicMyAsyncTask2(Buttonbutton1,ProgressBarpb,TextViewtv,intposition)
{
this.button1=button1;
this.pb=pb;
this.tv=tv;
this.position=position; } publicvoidsetReplace(ProgressBarpb,Buttonbutton1,TextViewtv) { this.pb=pb; this.button1=button1; this.tv=tv; } @Override protectedStringdoInBackground(String...params){
try{ URLurl=newURL(params[0]); URLConnectionopenConnection=url.openConnection(); intcontentLength=openConnection.getContentLength(); myint.put(position,contentLength); pb.setMax(contentLength); InputStreaminputStream=openConnection.getInputStream(); byte[]buffer=newbyte[1024]; intsum=0; intlen=0; hashmap.put(position,false); while(-1!=(len
=inputStream.read(buffer))) {
sum+=len;
Thread.sleep(100);
publishProgress(sum); } hashmap.put(position,true); }catch(MalformedURLExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }catch(InterruptedExceptione){ e.printStackTrace(); } return"下载完成"; } @Override protectedvoidonPostExecute(Stringresult){
button1.setText(result);
button1.setEnabled(true); super.onPostExecute(result); } @Override protectedvoidonProgressUpdate(Integer...values){ intfp=listview.getFirstVisiblePosition(); intlp=listview.getLastVisiblePosition(); if(position>=fp&&position<=lp){ pb.setProgress(values[0]); tv.setText((int)((double)values[0]/(double)pb.getMax()*100)+"%"); } super.onProgressUpdate(values); }
}
classMyAsyncTaskextendsAsyncTask<String,Void,Bitmap>
{
intposition;
ImageViewp_w_picpathview;
ProgressBarpb; publicMyAsyncTask(ImageViewp_w_picpathview,intposition,ProgressBarpb){ this.p_w_picpathview=p_w_picpathview; this.position=position; this.pb=pb; } @Override protectedBitmapdoInBackground(String...params){ Bitmapbitmap=null;
try{ URLurl=newURL(params[0]); URLConnectionconnection=url.openConnection(); InputStreamis=connection.getInputStream(); bitma
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 婴儿用品跨境贸易的机遇与挑战
- 英语阅读竞赛试卷
- 车辆交易税费抵扣贷款本金协议范本
- 成都房产交易合同范本:房屋交易售后服务及保障措施
- 隧道支护处理方案
- 车间灯光节能方案模板
- 食品项目资金使用方案
- 塔吊操作人员劳务派遣与权益保障合同
- 公共停车场车位租用及停车秩序管理合同
- 藏式风格民宿客房装修设计监理合同
- 农药经营考试题及答案
- 标前合作合同范本
- 2025年初级钢筋工(五级)技能认定理论考试指导题库(含答案)
- 2025 年小学劳动技术新课程标准(2022 版)标准试题
- 国家开放大学汉语言文学本科《古代诗歌散文专题》期末纸质考试第四大题论述题库2025春期版
- 2024秋新科粤版化学九年级上册教学课件 2.2 构成物质的微观粒子 第4课时 相对原子质量 离子的形成
- 魅力沟通技巧课件
- 国家开放大学法律事务专科《民法学(1)》期末纸质考试总题库2025春期考试版
- 2025年浙江安邦护卫集团有限公司招聘笔试参考题库含答案解析
- 2025年度资料员劳动合同范本(含试用期管理规定)
- 2024年06月浙江浙江泰隆商业银行社会招考笔试历年参考题库附带答案详解
评论
0/150
提交评论