在列表视图中更新进度条

6

我有一个场景,在我的聊天应用程序中,我想发送照片,但发送照片需要时间。我已经在我的应用程序中添加了一个进度条(水平),以显示发送照片的进度。但是,由于它是自定义列表视图的一项,所以我无法更新进度条。

这是我的asynchtask,用于设置进度条的进度,但我无法获得它。

 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);
        }
    }

进度条是自定义列表视图的一部分,我希望在运行时更新它。谢谢。
2个回答

7

您正在onProgress回调中填充ProgressBar。这是绝对错误的。在AsyncTask开始之前(inflate和add),应该将ProgressBar添加到视图中,然后在AsyncTask构造函数中将其设置为可见。您还应该在AsyncTask中存储ProgressBar的引用(或以某种非静态内部类的方式在作用域中)。

最终,您的AsyncTask应该像这样:

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]); 
    }
}

另外,在您的onProgress回调中,您不需要(也不应该)调用adapter.notifiyDataSetChanged()


我应该从getView方法中调用它吗? - Gaurav Arora
这个决定很大程度上取决于您的应用程序软件架构。没有对错之分。更像是在哪里调用您的AsyncTask更有意义。恐怕我不能为您回答这个问题。 - Taig

0

我想每个列表项视图都有一个进度条,如果是这样的话,您可以使用getView来获取该位置的视图,然后调用:

        View view = customAdapter1.getView(position, convertView, parent);
        ProgressBar p_Bar = (ProgressBar)view.findViewById(R.id.progress_bar_image);
        p_Bar.setProgress(Integer.parseInt(values[0]));

不需要调用customAdapter1.notifyDataSetChanged();


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接