0
リストビューにプログレスバーのダウンロードを追加しようとしました。ここに私のコードは、それが更新プログレスバーにlistViewのスクロール時にlistView.getChildAt(int index)がNULLを返します(Android)
ProgressBar progress = (ProgressBar) v.findViewById(R.id.progressBar2);
にエラーNullPointerExceptionが表示さ AdapterLv.class
public class AdapterPr extends BaseAdapter {
Activity activity;
ArrayList<String> listUrl;
LayoutInflater layoutInflater;
Clickdownload clickdownload;
ListView lstView;
private Handler handler= new Handler();
private String NameOfFolder="/DownLoadList1";
public AdapterPr(Activity activity, ArrayList<String> listUrl, ListView lstView) {
this.activity = activity;
this.listUrl = listUrl;
layoutInflater = LayoutInflater.from(activity);
this.lstView = lstView;
}
@Override
public int getCount() {
return listUrl.size();
}
@Override
public Object getItem(int i) {
return listUrl.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
View v = layoutInflater.inflate(R.layout.item, viewGroup, false);
Button download = (Button) v.findViewById(R.id.download);
TextView tvname = (TextView) v.findViewById(R.id.tvName);
tvname.setText("Itemt" + i);
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startDownload(i,listUrl.get(i));
}
});
return v;
}
private void updateStatus(int index, int Status) {
View v = lstView.getChildAt(index - lstView.getFirstVisiblePosition());
ProgressBar progress = (ProgressBar) v.findViewById(R.id.progressBar2);
progress.setProgress(Status);
TextView txtStatus = (TextView) v.findViewById(R.id.textView);
txtStatus.setText("Load : " + String.valueOf(Status) + "%");
}
public String subName(String data) {
return data.substring(data.lastIndexOf("/") + 1);
}
public void startDownload(final int position,final String urlDownload) {
Runnable runnable = new Runnable() {
int Status = 0;
public void run() {
int count = 0;
try {
URL url = new URL(urlDownload);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + NameOfFolder;
File dir = new File(file_path);
if (!dir.exists()) {
dir.mkdir();
}
File file = new File(dir, subName(urlDownload));
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
Status = (int)((total*100)/lenghtOfFile);
output.write(data, 0, count);
handler.post(new Runnable() {
public void run() {
updateStatus(position,Status);
}
});
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
}
};
new Thread(runnable).start();
}}
しかし、時にアイテムのダウンロードを隠す場合、私はリストビューをスクロール(画面に表示されていない)です。 どうすれば修正できますか? ありがとうございます。代わりに、クリックされたListView
行からプログレスバーを取得し、パラメータとしてstartDownload
方法でProgressBar
を追加し、updateStatus
にそれを渡すためにgetFirstVisiblePosition
を使用しての
は、あなたがログインしようとしているあなたの 'インデックス 'と' lstView.getFirstVisiblePosition() 'の返された出力は? –
あなたの問題は解決しましたか? –
@ShreeKrishnaリストビューをスクロールしないとうまく動作します。ログはありません –