版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026江西吉安白鹭洲中学面向高校招聘教师15人备考题库及答案详解(新)
- 2025江苏省体育科学研究所招聘专业技术人员3人备考题库及答案详解(新)
- 2026河北邯郸市曲周县医院招聘人事代理人员26人备考题库及参考答案详解
- 2026河南郑州大学河南省数字组工工程技术研究中心面向社会招聘非事业编制(劳务派遣)1人备考题库完整答案详解
- 2026江苏苏州市教育局直属学校招聘教师44人备考题库及答案详解(考点梳理)
- 2026大唐云南发电有限公司招聘备考题库及一套完整答案详解
- 2026年合肥共达职业技术学院专任教师公开招聘12名备考题库及答案详解一套
- 2026四川省医学科学院四川省人民医院专职科研人员、工程师招聘3人备考题库(二)及完整答案详解1套
- 企业能源管理与节能降耗指南(标准版)
- 未来五年绿化苗木企业ESG实践与创新战略分析研究报告
- 不良资产合作战略框架协议文本
- 2025年盐城中考历史试卷及答案
- 2026年孝昌县供水有限公司公开招聘正式员工备考题库完整参考答案详解
- 2025年郑州工业应用技术学院马克思主义基本原理概论期末考试模拟试卷
- 测绘资料档案汇交制度
- 2025年六年级上册道德与法治期末测试卷附答案(完整版)
- IPC7711C7721C-2017(CN)电子组件的返工修改和维修(完整版)
- 吕国泰《电子技术》
- 哈萨克族主要部落及其历史
- 2015比赛练习任务指导书
- 人教版七年级语文上册期末专题复习文言文训练及答案
评论
0/150
提交评论