2013-01-02 41 views
0

你好下面是一個方法,其中fileUpload方法被調用,上傳文件後我想刪除​​對象,我這樣做了。現在我必須通過調用fillRecipients()方法重新加載頁面,它是從數據庫中列出所有信息並顯示在listView中。因爲我已經用它的線程不列入允許我把fillRecipeints線程內,但我想它在行中沒有230評論以下爲號線230]下面 這是我的Synchronize方法:在android中調用線程中的方法

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

任何機構可以幫助我嗎?

+0

您是否試過使用Android AsyncTask? – itsrajesh4uguys

+0

我們不能這樣做...如果我不得不改變AsyncTask比我改變我的代碼 – ugene

+1

1.你想從數據庫中獲得所有細節..對於這個你可以使用Async任務有沒問題....然後在後期執行你可以使用fillRecipients()方法。其實你正試圖調用progressdialog下的線程權..所以,而不是你可以使用AsyncTask – itsrajesh4uguys

回答

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); 
     } 
    } 
相關問題