2016-03-15 133 views
0

我有一個listview類型的對話框彈出。當我點擊列表視圖的項目時,我想要加載進度條來顯示。以下是列表視圖代碼。如何在列表視圖的對話框頂部顯示加載進度條?

public AdapterView.OnItemClickListener getOnItemClickListener() { 
     return new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
//    ProgressDialog dialog; 
//    dialog = ProgressDialog.show(view.getContext(), "Please wait..", "Loading data", true); 
//    dialog.setCancelable(false); 
//    dialog.show(); 
       HashSet sometable= new HashSet(); 
       sometable.add(i); 
       if (getContext() == null) return; 
       Intent output = new Intent(); 
       final IData data = (IData) adapterView.getItemAtPosition(i); 
       //check if its myself 
       if((data.get("FIRST_NAME")==null)&&(data.get("LAST_NAME")==null)) 
       output.putExtra("User",""); 
       else{ 
        output.putExtra("User", (String) data.get("ASSIGNEE_GUID")); 
        output.putExtra("userName", (String) data.get("FIRST_NAME") + " " + (String) data.get("LAST_NAME")); 
        //new cancelPending((Activity) getContext(),data).execute(); 
        AssigneABO.SyncSubordinateCalendar((String) data.get("ASSIGNEE_GUID"), view.getContext()); 
        //new MyTask(MainActivity.this).execute((Void) null); 

       } 
       Activity activity = (Activity) getContext() ; 
       activity.setResult(Activity.RESULT_OK,output); 
       activity.finish(); 
      } 
     }; 
    } 

當我點擊listview的項目,啓動異步任務來獲取數據。在這個異步任務的onprecute方法中,我正在調用如下的進度對話框。

public static class SyncApplicationTask extends AsyncTask<String, Context, Boolean> { 
     Context context; 
     ProgressDialog progress;// = new ProgressDialog(context); 
     public SyncApplicationTask(Context mcontext) { 
      super(); 
      context = mcontext; 
      progress = new ProgressDialog(context); 
     } 

     @Override 
     protected void onPreExecute() { 

      progress.setMessage(context.getString(R.string.Sync)); 
      progress.setTitle(R.string.sync_msg); 
      progress.setCanceledOnTouchOutside(false); 
      progress.setCancelable(false); 
      progress.setIndeterminate(true); 
      progress.show(); 

      if (!Util.hasActiveNetworkConnection(context)) { 
       Util.alertMessageDialog(context, R.string.network_connection_unavailable_message); 
      return; 
      } 
      if (SynchronizationManager.getInstance().isSyncAllCategoryGroupActive()) { 
       Util.alertMessageDialog(context, R.string.synchronization_still_active); 
       return; 
      } 
      if (!ConnectionManager.getInstance().isServerConnected()) { 
       ConnectionManager.getInstance().stopServerConnection(60); 
       boolean isStarted = ConnectionManager.getInstance() 
         .startServerConnection(60); // 2 mins time out 
       Log.v("Connection started : ", Boolean.valueOf(isStarted) 
         .toString()); 
       if (!isStarted) { 
        Util.alertMessageDialog(context, R.string.connection_server_lost_message); 
        return; 
       } 
      } 
     } 

     @Override 
     protected Boolean doInBackground(String... syncGroups) { 
      try { 
       SAPReXDB.synchronize(syncGroups[0]); 

       return true; 
      }catch (Exception Ex) 
      { Log.v("Erron on sync: ", Ex.toString()); 
       return false; 
      } 
     } 

     @Override 
     protected void onPostExecute(final Boolean c) { 
      progress.dismiss(); 
      if(!c) 
       Util.alertMessageDialog(context, R.string.sync_progress_failed); 

     } 
    } 

但它顯示在對話列表視圖後面。我希望它顯示在對話列表視圖的上方。我究竟做錯了什麼。你能否就此提出建議。謝謝。

+0

你可以試試這個:http://stackoverflow.com/a/6758800/5124783 –

回答

1

使用哪個上下文創建ProgressDialog?
GUI位置&當您不使用正常的活動上下文,而是使用特定視圖的上下文或應用程序上下文時,可能會發生訂單問題。

+0

你好@Suhyeon,我是listview的視圖來獲取像view.getContext()的上下文。 – coders

+0

@coders您可以使用當前Activity的上下文對其進行測試嗎?通過臨時傳遞Activity Context或將其設置爲public? –

+0

@coders如果使用列表視圖前面的佈局的上下文,那麼至少應該顯示進度(如果位置正確) –

相關問題