私はGridView
の書籍タイトルとダウンロードボタンを含むセルを持っています。私は、ダウンロードボタンが消えて、そのボタンが押されたときに進行状況バーが表示されるようにしたい。同時に私はファイルのダウンロードを開始し、ダウンロードから更新が得られたらプログレスバーを更新したいと思う。最後にダウンロードが完了したら、進行状況バーを非表示にしてReadボタンを表示します。GridViewのプログレスバーを正しく更新する方法は?
私は自分自身には、次のGetViewメソッドで、カスタムアダプタを作成することである。この構造化しまし方法:以下のdownloadFile
方法で
public View getView(final int position, View convertView, ViewGroup parent) {
final RelativeLayout card;
if (convertView == null) {
card = (RelativeLayout) getLayoutInflater().inflate(R.layout.book_cover, null);
} else {
card = (RelativeLayout) convertView;
}
TextView bookName = (TextView) card.findViewById(R.id.textView_bookName);
final MNBook currentBook = getItem(position);
bookName.setText((String) currentBook.getTitle());
final Button downloadBookButton = (Button) card.findViewById(R.id.button_download_book);
final Button readBookButton = (Button) card.findViewById(R.id.button_read_book);
final ProgressBar progressBar = (ProgressBar) card.findViewById(R.id.progressbar_book_download);
downloadBookButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadFile(currentBook, progressBar,
downloadBookButton, readBookButton);
}
});
if (currentBook.isBooleanDownloaded()) {
downloadBookButton.setVisibility(View.GONE);
readBookButton.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
}
return card;
}
:
private void downloadFile(MNBook book, final ProgressBar progressBar,
final Button downloadButton, final Button readButton) {
final MNBook localBook = book;
String fileName = book.getFileName();
String url = book.getURL();
Intent intent = new Intent(this, DownloadService.class);
intent.putExtra("url", url);
intent.putExtra("file_name", fileName);
intent.putExtra("receiver", new ResultReceiver(new Handler()) {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
if (resultCode == DownloadService.UPDATE_PROGRESS) {
int progress = resultData.getInt("progress");
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE);
readButton.setVisibility(View.VISIBLE);
setDownloadedBookStatus(localBook);
}
}
}
});
downloadButton.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
startService(intent);
}
私が持っている問題をこのアプローチでは、プログレスバーが他のセルにジャンプします。基本的に、1つの本でダウンロードして下にスクロールすると、進捗状況が別の本に表示されます。
この問題を解決する方法を知りたいと思います。私のコードを設定する別の方法があれば、私にとっては大丈夫です。
(R/androidquestions /にクロスポスト)
進捗状況を更新するたびに 'notifyDataSetChanged()'を呼び出した結果、私はもはや新しいダウンロードを開始できなくなったことに気付きました。他のユーザーが現在ダウンロードしているときにダウンロードボタンを押すと応答がありません。私は自由にスクロールすることができるので、私は何かをダウンロードしているときに、アプリケーション自体はまだ応答しています。あなたはこの問題を解決する方法を知っていますか? – maesydy
これは別の問題だと思うし、適切な答えを得るために、より多くの背景情報を持つ新しい質問が必要になるかもしれません。クイックアイデアは、バックグラウンドでサービスを使用してダウンロードしているように見えます。同時に1つのタイプのサービスしか実行できないので、すでに何かをダウンロードしていて、内部でキューイングを処理していない場合このサービスは失敗します。もう一度、私はコードの残りの部分を知らないので、それはちょうど推測です。 –