Android 当活动重新输入Set ProgressValue时,在GridView内设置ProgressBar
亲爱的斯塔克沃夫人 我有一个Gridview,每个项目都有一个水平进度条,在项目按钮点击时可见 我通过使用asyncTask来实现它,但现在我的问题是 1) 当我在前进时向上或向下滚动时,Progressbar会在可视的其他项目中前进,这与位置更改有关 2) 正在更改活动并再次访问此活动进度条在该网格项中不可见,但在后台运行的进度我在Logcat中检查它 这里是我的流程示例 这是下载进度的代码Android 当活动重新输入Set ProgressValue时,在GridView内设置ProgressBar,android,gridview,Android,Gridview,亲爱的斯塔克沃夫人 我有一个Gridview,每个项目都有一个水平进度条,在项目按钮点击时可见 我通过使用asyncTask来实现它,但现在我的问题是 1) 当我在前进时向上或向下滚动时,Progressbar会在可视的其他项目中前进,这与位置更改有关 2) 正在更改活动并再次访问此活动进度条在该网格项中不可见,但在后台运行的进度我在Logcat中检查它 这里是我的流程示例 这是下载进度的代码 private void UpdateDB(String strFilename,int lintIs
private void UpdateDB(String strFilename,int lintIssueId,boolean bPreview,ImageView btnDownload,ImageView btnView)
{
try{
btnDownload.setVisibility(View.GONE);
btnView.setVisibility(View.VISIBLE);
}catch(Exception ex){}
}
private static class SCSDownload extends AsyncTask<String, Integer, String>
{
Main_Page activity;
MiddlewareInterface AMI=MiddlewareInterface.GetInstance();
ProgressBar mProgressbar;
RelativeLayout mRtProgress;
ImageView btnDownload,btnView;
int issueid;
boolean bPrev;
Context context;
SCSDownload(Main_Page act,ProgressBar mProgressbar,RelativeLayout mRtProgress,ImageView btnDownload,ImageView btnView,int issueid,boolean bPrev)
{
this.mProgressbar=mProgressbar;
this.mRtProgress=mRtProgress;
this.issueid=issueid;
this.bPrev=bPrev;
this.btnView=btnView;
this.btnDownload=btnDownload;
attatch(act);
}
void attatch(Main_Page act)
{
activity=act;
context=act;
}
void detatch()
{
activity=null;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
mRtProgress.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
if(result!=null)
{
try{
if(mRtProgress!=null)
mRtProgress.setVisibility(View.GONE);
activity.UpdateDB(result,issueid,bPrev,btnDownload,btnView);
}catch(Exception e){}
}
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
mProgressbar.setProgress(values[0]);
super.onProgressUpdate(values);
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try{
String File_Name=params[0].substring( params[0].lastIndexOf('/')+1, params[0].length() );
File file = new File(context.getDir(AMI.strMainDir, Context.MODE_PRIVATE) + "/"+File_Name);
if (!file.exists())
{
file.createNewFile();
URL url=new URL(params[0]);
URLConnection con=url.openConnection();
con.connect();
int LengthOfFile=con.getContentLength();
InputStream input=new BufferedInputStream(url.openStream());
OutputStream output=new FileOutputStream(file);
byte data[]=new byte[1024];
int count = 0;
long total=0;
while((count=input.read(data))!=-1&&(!isCancelled()))
{
total+=count;
Log.d("total",total+"");
publishProgress((int)((total*100)/LengthOfFile));
output.write(data,0,count);
}
output.flush();
output.close();
input.close();
}
return File_Name;
}catch(Exception ex){}
return null;
}
}
private void UpdateDB(字符串strFilename、int lintsissueid、boolean bPreview、ImageView btnDownload、ImageView btnView)
{
试一试{
btnDownload.setVisibility(View.GONE);
btnView.setVisibility(View.VISIBLE);
}捕获(例外情况除外){}
}
私有静态类SCSDownload扩展异步任务
{
主页活动;
MiddlewareInterface AMI=MiddlewareInterface.GetInstance();
ProgressBar-mProgressbar;
相对进展;
图像视图BTN下载,BTN查看;
国际发行ID;
布尔bPrev;
语境;
SCSDownload(主页行为、进度栏mProgressbar、RelativeLayout mRtProgress、ImageView Btnownload、ImageView btnView、int issueid、布尔bPrev)
{
这个.mProgressbar=mProgressbar;
this.mRtProgress=mRtProgress;
this.issueid=issueid;
这个.bPrev=bPrev;
this.btnView=btnView;
这个.btnDownload=btnDownload;
附件(法案);
}
无效附件(主页法)
{
活动=行动;
上下文=行为;
}
void detatch()
{
活动=空;
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
mRtProgress.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
如果(结果!=null)
{
试一试{
if(mRtProgress!=null)
mRtProgress.setVisibility(View.GONE);
activity.UpdateDB(结果、发布ID、bPrev、btnDownload、btnView);
}捕获(例外e){}
}
super.onPostExecute(结果);
}
@凌驾
受保护的void onProgressUpdate(整型…值){
//TODO自动生成的方法存根
mProgressbar.setProgress(值[0]);
super.onProgressUpdate(值);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
试一试{
字符串文件_Name=params[0]。子字符串(params[0]。lastIndexOf('/')+1,params[0]。length();
File File=新文件(context.getDir(AMI.strMainDir,context.MODE\u PRIVATE)+“/”+文件名);
如果(!file.exists())
{
createNewFile();
URL=新URL(参数[0]);
URLConnection con=url.openConnection();
con.connect();
int LengthOfFile=con.getContentLength();
InputStream输入=新的BufferedInputStream(url.openStream());
OutputStream output=新文件OutputStream(文件);
字节数据[]=新字节[1024];
整数计数=0;
长总计=0;
而((count=input.read(data))!=-1&(!isCancelled())
{
总数+=计数;
Log.d(“总计”,总计+”);
出版进度((整数)((总计*100)/长度办公室);
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
}
返回文件名;
}捕获(例外情况除外){}
返回null;
}
}
我也在寻找解决方案