我在 listview 项目中有按钮,当它被单击时,水平进度条开始加载,当它完成加载时,两个按钮应该出现在 listview 的那个项目中。在下载进度条时,如果我将该项目从屏幕上滚动然后向后滚动,进度条就会消失并出现两个按钮。下载过程正常。但是我想一直在下载时显示进度条?这是我的适配器。请任何帮助。提前致谢
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ArrayList<String> array=JournalArray.get(position);
LayoutInflater inflator = ((Activity) activity).getLayoutInflater();
final Hashtable<Integer, Integer> hash = new Hashtable<Integer, Integer>();
final Button imgAR;
final Button imgPDF;
final Button imgDown;
final Button btnDel;
final TextProgressBar progress;
//ConnectionDetector cd=new ConnectionDetector(activity);
final Database db=new Database(activity);
//final ImageView imgViewFlag;
//final TextView txtViewTitle,txtProgress;
if(convertView==null)
{
convertView = inflator.inflate(R.layout.familylist_item, null);
}
progress=(TextProgressBar)convertView.findViewById(R.id.downprogress);
progress.setTextColor(Color.BLACK);
//progress.setTag(tag)
//progress.setVisibility(View.GONE);
TextView txtViewTitle = (TextView) convertView.findViewById(R.id.text);
//view.txtProgress=(TextView)convertView.findViewById(R.id.textProgress);
ImageView imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
imgAR=(Button)convertView.findViewById(R.id.imageAR);
//view.imgAR.setTag(view);
imgDown=(Button)convertView.findViewById(R.id.imageDown);
//view.imgDown.setTag(view);
imgPDF=(Button)convertView.findViewById(R.id.imagePDF);
//view.imgPDF.setTag(view);
//view.progress=(ProgressBar)convertView.findViewById(R.id.downprogress);
btnDel=(Button)convertView.findViewById(R.id.btnDel);
//view.btnDel.setTag(view);
//convertView.setTag(view);
if(DeleteBin){
if(XMLParser.CheckFileInSD(array.get(5))){
btnDel.setVisibility(View.VISIBLE);
imgAR.setVisibility(View.GONE);
imgDown.setVisibility(View.GONE);
imgPDF.setVisibility(View.GONE);
progress.setVisibility(View.GONE);
}
else{
imgDown.setVisibility(View.GONE);
imgAR.setVisibility(View.GONE);
btnDel.setVisibility(View.GONE);
imgPDF.setVisibility(View.GONE);
progress.setVisibility(View.GONE);
}
}
else{
btnDel.setVisibility(View.GONE);
if(!XMLParser.CheckFileInSD(array.get(5))){
imgAR.setVisibility(View.GONE);
imgPDF.setVisibility(View.GONE);
imgDown.setVisibility(View.VISIBLE);
progress.setVisibility(View.GONE);
}
else{
imgAR.setVisibility(View.VISIBLE);
imgPDF.setVisibility(View.VISIBLE);
imgDown.setVisibility(View.GONE);
progress.setVisibility(View.GONE);
}
}
txtViewTitle.setText(array.get(1));
channelid=array.get(4);
RelativeLayout.LayoutParams paramrel = new RelativeLayout.LayoutParams(width/5, height/5);
paramrel.leftMargin = width/10;
imgViewFlag.setLayoutParams(paramrel);
imgViewFlag.setImageDrawable(Drawable.createFromPath(new File(array.get(3)).getAbsolutePath()));
btnDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pos=position;
Deletefiles(pos);
Toast.makeText(activity, "Сэтгүүл амжилттай усгтгагдлаа", Toast.LENGTH_SHORT).show();
btnDel.setVisibility(View.GONE);
}
});
imgAR.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
channelid=array.get(4);
Intent intent=new Intent(activity,ARELViewActivity.class);
intent.putExtra("channelid", db.getConfigByChannelID(channelid));
activity.startActivity(intent);
}
});
imgPDF.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SdCardPath")
@Override
public void onClick(View v) {
path=array.get(5);
String PATH=Environment.getExternalStorageDirectory().getAbsolutePath()+ "/sdcard/.FamilyMagazine/";
openBook(PATH+XMLParser.getFileName(path));
}
});
imgDown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Database db=new Database(activity);
final ConnectionDetector cd=new ConnectionDetector(activity);
if(downloading<3){
downloading++;
path=array.get(5);
channelid=array.get(4);
pos=position;
progress.setMax(100);
progress.setProgress(0);
progress.setVisibility(View.VISIBLE);
imgDown.setVisibility(View.GONE);
progress.setText("Татаж байна...");
new AsyncTask<View, String, String>() {
private View v;
@Override
protected String doInBackground(View... params) {
v = params[0];
int count;
isLoading=true;
//mprogress=progress;
String result="0";
if(cd.isConnectingToInternet()){
getChannel(channelid);
try {
URL url = new URL(path);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
String PATH="/mnt/sdcard/sdcard/.FamilyMagazine";
File dir=new File(PATH);
boolean test=dir.mkdirs();
OutputStream output = new FileOutputStream("/mnt/sdcard/sdcard/.FamilyMagazine/"+XMLParser.getFileName(path));
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
hash.put(pos, (int)((total*100)/lenghtOfFile));
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
result=e.toString();
}
}
else
result="Интернетэд холбогдоогүй байна!";
return result;
}
protected void onProgressUpdate(String... progresses) {
// setting progress percentage
progress.setProgress(Integer.parseInt(progresses[0]));
int x=lenghtOfFile/100 * Integer.parseInt(progresses[0])/1048576;
progress.setText(x+"MB / "+lenghtOfFile/1048576+"MB");
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//view.txtProgress.setVisibility(View.GONE);
downloading--;
isLoading=false;
if(result.equals("0")){
imgDown.setVisibility(View.GONE);
progress.setVisibility(View.GONE);
btnDel.setVisibility(View.GONE);
imgAR.setVisibility(View.VISIBLE);
imgPDF.setVisibility(View.VISIBLE);
}
else
{
Toast.makeText(activity, result, Toast.LENGTH_LONG).show();
imgDown.setVisibility(View.VISIBLE);
btnDel.setVisibility(View.GONE);
imgAR.setVisibility(View.GONE);
imgPDF.setVisibility(View.GONE);
}
}
}.execute(v);
}
else
Toast.makeText(activity, "Өөр сэтгүүл татаж байна\n Түр хүлээнэ үү", Toast.LENGTH_LONG).show();
}
});
return convertView;
}