我有一个带基础适配器的列表视图。列表视图中的每一行都包含图像,标题,下载和查看按钮以及进度条。最初进度条和查看按钮的可见性是GONE。当用户按下载按钮时,进度条应该是可见的。下载完成后,下载按钮应该消失,查看按钮应该可见。
我的问题是:我无法从asynctask更改视图的可见性。
这是我的代码。
public class PdfListAdapter extends BaseAdapter {
ArrayList<PdfDetails> arylstPdf = new ArrayList<PdfDetails>();
Context context;
String extStorageDirectory;
ViewHolder holder;
Activity activity;
public PdfListAdapter(Context context, ArrayList<PdfDetails> arylstPdf) {
super();
this.arylstPdf = arylstPdf;
this.context = context;
extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
holder = new ViewHolder();
}
@Override
public int getCount() {
return arylstPdf.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = LayoutInflater.from(context);
activity = (Activity) context;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.layout_pdf_list, null);
holder.tvPdfTitle = (TextView) convertView
.findViewById(R.id.tvPdfTitle);
holder.imgPdfImage = (ImageView) convertView
.findViewById(R.id.imgPdfImage);
holder.btnDownload = (Button) convertView
.findViewById(R.id.btnDownload);
holder.btnView = (Button) convertView.findViewById(R.id.btnView);
holder.pbDownload = (ProgressBar) convertView
.findViewById(R.id.pbDownload);
holder.tvProgress = (TextView) convertView.findViewById(R.id.tvProgress);
holder.llProgress = (LinearLayout) convertView.findViewById(R.id.llProgress);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
File file = new File(extStorageDirectory + "/pdf", arylstPdf.get(
position).getPostTitle()
+ ".pdf");
if (file.exists()) {
holder.btnDownload.setVisibility(View.GONE);
holder.btnView.setVisibility(View.VISIBLE);
} else {
holder.btnDownload.setVisibility(View.VISIBLE);
holder.btnView.setVisibility(View.GONE);
}
holder.tvPdfTitle.setText(arylstPdf.get(position).getPostTitle());
ImageLoader objImageLoader = new ImageLoader(context);
objImageLoader.DisplayImage(arylstPdf.get(position).getAttachedImage(),
holder.imgPdfImage);
holder.btnDownload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// NOT WORKING
holder.llProgress.setVisibility(View.VISIBLE);
Async async = new Async();
async.execute(Integer.toString(position));
}
});
holder.btnView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
readPDF(arylstPdf.get(position).getPostTitle());
}
});
return convertView;
}
class ViewHolder {
ImageView imgPdfImage;
TextView tvPdfTitle, tvProgress;
Button btnDownload;
Button btnView;
ProgressBar pbDownload;
LinearLayout llProgress;
}
class Async extends AsyncTask<String, String, String> {
File file, folder;
@Override
protected void onPreExecute() {
super.onPreExecute();
folder = new File(extStorageDirectory, "pdf");
folder.mkdir();
}
@Override
protected String doInBackground(String... params) {
String fileName = arylstPdf.get(Integer.parseInt(params[0]))
.getPostTitle();
file = new File(folder, fileName + ".pdf");
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
int count;
try {
URL url = new URL(arylstPdf.get(Integer.parseInt(params[0]))
.getAttachedPdf());
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(),
8192);
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(Integer
.toString((int) ((total * 100) / lenghtOfFile)));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... progress) {
holder.tvProgress.setText(progress[0]);
holder.pbDownload.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// NOT WORKING
holder.btnDownload.setVisibility(View.GONE);
holder.btnDownload.setVisibility(View.VISIBLE);
Toast.makeText(context, "Downloaded", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:2)
在PdfDetails
类更改下载和getView
方法中查看按钮可见性中添加布尔标记。
所以只需在arraylist位置更改该特定行的布尔标志。
和用户adapter.notifyDataStateChanged();
在PdfDetails类中添加getter setter方法。
和getView()方法
使用
PdfDetails detailBin = list.get(position);
if(detailBin.isDownloaded)
// view button visible and download button hide
else
// download button visible and view button hide
并在postExecute()
中list.get(position).setDownload(true);
adapter.notifyDataStateChanged();
答案 1 :(得分:0)
您可以将它放在holder
本身内,而不是在Adapter类中创建getView
,并将其传递给AsyncTask
构造函数。
public class PdfListAdapter extends BaseAdapter {
ArrayList<PdfDetails> arylstPdf = new ArrayList<PdfDetails>();
Context context;
String extStorageDirectory;
Activity activity;
public PdfListAdapter(Context context, ArrayList<PdfDetails> arylstPdf) {
super();
this.arylstPdf = arylstPdf;
this.context = context;
extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
}
@Override
public int getCount() {
return arylstPdf.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = LayoutInflater.from(context);
activity = (Activity) context;
final ViewHolder holder= null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.layout_pdf_list, null);
holder = new ViewHolder();
holder.tvPdfTitle = (TextView) convertView
.findViewById(R.id.tvPdfTitle);
holder.imgPdfImage = (ImageView) convertView
.findViewById(R.id.imgPdfImage);
holder.btnDownload = (Button) convertView
.findViewById(R.id.btnDownload);
holder.btnView = (Button) convertView.findViewById(R.id.btnView);
holder.pbDownload = (ProgressBar) convertView
.findViewById(R.id.pbDownload);
holder.tvProgress = (TextView) convertView.findViewById(R.id.tvProgress);
holder.llProgress = (LinearLayout) convertView.findViewById(R.id.llProgress);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
File file = new File(extStorageDirectory + "/pdf", arylstPdf.get(
position).getPostTitle()
+ ".pdf");
if (file.exists()) {
holder.btnDownload.setVisibility(View.GONE);
holder.btnView.setVisibility(View.VISIBLE);
} else {
holder.btnDownload.setVisibility(View.VISIBLE);
holder.btnView.setVisibility(View.GONE);
}
holder.tvPdfTitle.setText(arylstPdf.get(position).getPostTitle());
ImageLoader objImageLoader = new ImageLoader(context);
objImageLoader.DisplayImage(arylstPdf.get(position).getAttachedImage(),
holder.imgPdfImage);
holder.btnDownload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// NOT WORKING
holder.llProgress.setVisibility(View.VISIBLE);
Async async = new Async(holder);
async.execute(Integer.toString(position));
}
});
holder.btnView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
readPDF(arylstPdf.get(position).getPostTitle());
}
});
return convertView;
}
class ViewHolder {
ImageView imgPdfImage;
TextView tvPdfTitle, tvProgress;
Button btnDownload;
Button btnView;
ProgressBar pbDownload;
LinearLayout llProgress;
}
class Async extends AsyncTask<String, String, String> {
File file, folder;
ViewHolder holder;
public Async(ViewHolder holder) {
this.holder=holder;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
folder = new File(extStorageDirectory, "pdf");
folder.mkdir();
}
@Override
protected String doInBackground(String... params) {
String fileName = arylstPdf.get(Integer.parseInt(params[0]))
.getPostTitle();
file = new File(folder, fileName + ".pdf");
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
int count;
try {
URL url = new URL(arylstPdf.get(Integer.parseInt(params[0]))
.getAttachedPdf());
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(),
8192);
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(Integer
.toString((int) ((total * 100) / lenghtOfFile)));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... progress) {
holder.tvProgress.setText(progress[0]);
holder.pbDownload.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// NOT WORKING
holder.btnDownload.setVisibility(View.GONE);
holder.btnDownload.setVisibility(View.VISIBLE);
Toast.makeText(context, "Downloaded", Toast.LENGTH_SHORT).show();
}
}
}
答案 2 :(得分:0)
除了Main ui线程之外,您无法对其他线程(异步任务)中的用户界面(UI)进行任何更改。 所以你需要遵循这个
runOnUiThread(new Runnable() {
@Override
public void run() {
//Any UI changes can be done here
holder.btnDownload.setVisibility(View.GONE);
holder.btnView.setVisibility(View.VISIBLE);
}
});