0

您好,下面是一种fileUpload调用方法的方法,上传文件后我想删除该synchronized对象,我这样做了。现在我必须通过调用fillRecipients()方法重新加载页面,它的作用是列出数据库中的所有信息并显示在listView. 由于我使用了线程,它不允许我放入fillRecipeints线程内,但我希望它位于下面的第 230 行 [下面注释为第 230 行] 这是我的Synchronize方法:

public void synchronize(final String id){
    dialog = ProgressDialog.show(ViewRecipients.this, "", "Uploading this file...", true);
    new Thread(new Runnable() {
           public void run() {
                runOnUiThread(new Runnable() {
                       public void run() {
                           //uploading.setText("uploading started.....");
                           //dialog.show();
                       }
                   });
                mDbHelper.open();
                Cursor cData = mDbHelper.fetchRecipientInfo(id);

                for(cData.moveToFirst();!cData.isAfterLast();cData.moveToNext()){
                    String id = cData.getString(cData.getColumnIndex("fld_recipient_id"));
                    String info = cData.getString(cData.getColumnIndex("fld_info"));
                    String latitude = cData.getString(cData.getColumnIndex("fld_latitude"));
                    String longitude = cData.getString(cData.getColumnIndex("fld_longitude"));
                    ArrayList<String> imagesArray = new ArrayList<String>();
                    for (int i = 1; i <= 4; i++) {
                        String image = cData.getString(cData.getColumnIndex("fld_image_url" + i));
                        if (image != null) {
                            imagesArray.add(image);
                        }
                    }

                    try {
                        serverResponseCode = uploadFile(imagesArray, info, latitude, longitude, id);
                        if (serverResponseCode==200){
                              mDbHelper.deleteRecipientRecId(id);
//Line NO 230 here i want to add fillRecipients() method
                        }
                        dialog.dismiss();
                    } catch (IOException e) {
                        e.printStackTrace();
                        dialog.dismiss();
                    }
                }
                cData.close();
                mDbHelper.close();

                if(serverResponseCode == 200){
                     runOnUiThread(new Runnable() {
                          public void run() {
                              Toast.makeText(ViewRecipients.this, "File Upload Complete.", Toast.LENGTH_SHORT).show();
                          }
                      });               
                 }

           }
         }).start();
}

这是我的 fillRecipeints() 方法:

private void fillRecipients(){
    mCursor = mDbHelper.fetchAllRecipientsInfo();
    if (mCursor==null){
        System.out.println("empty cursor");
    }
    else{
        String [] from = new String[]{MunchaDbAdapter.FLD_RECIPIENT_ID};
        int [] to = new int[]{R.id.text1};
        SimpleCursorAdapter recipient = new SimpleCursorAdapter(this, R.layout.recipient_show, mCursor, from, to);
        setListAdapter(recipient);
    }
}

有谁能够帮我?

4

1 回答 1

0
public void synchronize(final String id) {

        new UploadAsync.execute();

    }


//async class to do task in background and notify UI after completion of task in onPost()
class UploadAsync extends AsyncTask<Void, Void, Void>{
        ProgressDialog dialog = null;
        boolean isUploaded = false;
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            ProgressDialog.show(ViewRecipients.this, "", "Uploading this file...", true);
        }

        @Override
        protected Void doInBackground(Void... params) {
            try{
            mDbHelper.open();
            Cursor cData = mDbHelper.fetchRecipientInfo(id);

            for(cData.moveToFirst();!cData.isAfterLast();cData.moveToNext()){
                String id = cData.getString(cData.getColumnIndex("fld_recipient_id"));
                String info = cData.getString(cData.getColumnIndex("fld_info"));
                String latitude = cData.getString(cData.getColumnIndex("fld_latitude"));
                String longitude = cData.getString(cData.getColumnIndex("fld_longitude"));
                ArrayList<String> imagesArray = new ArrayList<String>();
                for (int i = 1; i <= 4; i++) {
                    String image = cData.getString(cData.getColumnIndex("fld_image_url" + i));
                    if (image != null) {
                        imagesArray.add(image);
                    }
                }

                try {
                    serverResponseCode = uploadFile(imagesArray, info, latitude, longitude, id);
                    if (serverResponseCode==200){
                          mDbHelper.deleteRecipientRecId(id);
                          isUploaded = true;
                    }

                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            cData.close();
            mDbHelper.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            //close dialog
            if(dialog != null && dialog.isShowing()){
                dialog.dismiss();
            }


            if(isUploaded){
                Toast.makeText(ViewRecipients.this, "File Upload Complete.", Toast.LENGTH_SHORT).show();
                fillRecipients();
            }
        }

    }

private void fillRecipients() {
        mCursor = mDbHelper.fetchAllRecipientsInfo();
        if (mCursor == null) {
            System.out.println("empty cursor");
        } else {
            String[] from = new String[] { MunchaDbAdapter.FLD_RECIPIENT_ID };
            int[] to = new int[] { R.id.text1 };
            SimpleCursorAdapter recipient = new SimpleCursorAdapter(this,
                    R.layout.recipient_show, mCursor, from, to);
            setListAdapter(recipient);
        }
    }
于 2013-01-02T13:30:40.370 回答