我有一个场景,在我的聊天应用程序中我想发送照片,照片发送需要时间。我附上了一个进度条(水平)来显示在我的应用程序中发送照片的进度。但是我无法更新进度条,因为它是自定义 ListView 的项目。
这是我设置进度条进度的异步任务,但我无法获取它。
class SendPhotoToFriend extends AsyncTask<String, String, String>{
String receiver,path = "";
public SendPhotoToFriend(String path,String receiver) {
// TODO Auto-generated constructor stub
this.path = path;
this.receiver = receiver;
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
ServiceDiscoveryManager sdm = ServiceDiscoveryManager
.getInstanceFor(connection);
if (sdm == null)
sdm = new ServiceDiscoveryManager(connection);
sdm.addFeature("http://jabber.org/protocol/disco#info");
sdm.addFeature("jabber:iq:privacy");
// Create the file transfer manager
FileTransferManager manager = new FileTransferManager(
connection);
FileTransferNegotiator.setServiceEnabled(connection, true);
// Create the outgoing file transfer
OutgoingFileTransfer transfer = manager
.createOutgoingFileTransfer(receiver + "/Smack");
System.out.println("Receiver of the file is "+receiver+"/smack");
Log.i("transfere file", "outgoingfiletransfer is created");
try {
long total = 0;
OutgoingFileTransfer.setResponseTimeout(30000);
transfer.sendFile(new File(path), "Description");
Log.i("transfere file", "sending file");
while (!transfer.isDone()) {
//Thread.sleep(1000);
total+=transfer.getProgress();
publishProgress(""+(int)((total*100)/transfer.getFileSize()));
Log.i("transfere file", "sending file status "
+ transfer.getStatus() + "progress: "
+ transfer.getProgress());
if (transfer.getStatus() == org.jivesoftware.smackx.filetransfer.FileTransfer.Status.error) {
Log.i("transfere file", "Errorrr isss: "
+ transfer.getError()+transfer.getPeer());
transfer.cancel();
break;
}
}
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("transfere file", "sending file done");
return null;
}
@Override
protected void onProgressUpdate(final String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view =inflater.inflate(R.layout.user_chat_send_chat, null);
ProgressBar p_Bar = (ProgressBar)view.findViewById(R.id.progress_bar_image);
p_Bar.setProgress(Integer.parseInt(values[0]));
customAdapter1.notifyDataSetChanged();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
进度条是自定义 ListView 的 View ,我想在运行时更新。
谢谢
最佳答案
您正在 onProgress 回调中膨胀 ProgressBar。这是绝对错误的。 ProgressBar 应该在您的 AsyncTask 启动(膨胀和添加)之前添加到 View 中,然后将其转换为在您的 AsyncTask 构造函数中可见。您还应该在 AsyncTask 中存储对 ProgressBar 的引用(或以其他方式在非静态内部类的范围内)。
最后你的 AsyncTask 应该看起来像这样(假设 ProgressBar 已经附加到 View ):
class SendPhotoToFriend extends AsyncTask<String, String, String> {
String receiver,path = "";
ProgressBar progress;
public SendPhotoToFriend(String path, String receiver, ProgressBar progress) {
this.path = path;
this.receiver = receiver;
this.progress = progress;
this.progress.setVisibility(View.Visible);
}
@Override
protected String doInBackground(String... arg0) {
// You're application logic...
// ...
publishProgress((int)((total*100)/transfer.getFileSize()));
// ...
}
@Override
protected void onProgressUpdate(final Integer... values) {
this.progress.setProgress(values[0]);
}
}
此外,您不需要(也不应该)在 onProgres-Callback 中调用 adapter.notifiyDataSetChanged()
。
关于android - 更新 ListView 中的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14685322/