2016-02-08 31 views
0

我有一個創建調用異步任務的活動。根據asyntask結果更新不同片段的文字瀏覽

public class Sync extends BaseActivity implements OnClickListener{ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_sync); 
     CheckForDeviceTask(Sync.this).execute(Constants.DEVICE_ADDRESS); 
    } 
} 

現在異步任務的結果決定了哪個片段要加載到我能夠做的活動中。但無法計算如何更新片段內的文字瀏覽。請看看onPostExecute()方法。加載一個片段後,我如何根據結果更新TextViews?

public class CheckForDeviceTask extends AsyncTask<String, Integer, String> { 
    private static final String TAG = "CheckForDeviceTask"; 
    public static final int CONNECTED_DEVICE_EXISTS = 1; 
    public static final int WAITING_TEMP_PASSWORD = 2; 
    private Activity activity; 

    // private DialogFragment dialogFragment; 

    public CheckForDeviceTask(Activity activity) { 
     super(); 
     this.activity = activity; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // FragmentManager fm = activity.getFragmentManager(); 
     // dialogFragment = ProgressDialogFragment.newInstance("Saving"); 
     // dialogFragment.show(fm, "dialog"); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     if (GCHUtils.isConnected(activity)) { 
      return GCHUtils.httpGet(RestApiPaths.CHECK_FOR_DEVICE 
        + params[0].toString()); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // dialogFragment.dismiss(); 
     try { 
      if (result != null && !result.isEmpty()) { 
       Map<String, Object> resultMap = MapUtility.jsonToMap(result); 
       if (resultMap != null && !resultMap.isEmpty()) { 
        Log.d(TAG, resultMap.get("status").toString()); 
        FragmentManager fm; 
        FragmentTransaction ft; 
        if (Boolean.valueOf(resultMap.get("status").toString())) { 
         int type = Integer.valueOf(resultMap.get("type") 
           .toString()); 
         switch (type) { 
         case CONNECTED_DEVICE_EXISTS: { 
          GCHConnectedFragment connectedGCHFragment = new GCHConnectedFragment(); 
          fm = activity.getFragmentManager(); 
          ft = fm.beginTransaction(); 
          ft.replace(R.id.layoutToReplace, 
            connectedGCHFragment); 
          ft.commit(); 
          String email = resultMap.get("emailId").toString(); 
          String name = resultMap.get("name").toString(); 
          // now I need to update two TextViews in GCHConnectedFragment based with these values 
         } 
          break; 
         case WAITING_TEMP_PASSWORD: { 

          TempPasswordFragment tempPasswordFragment = new TempPasswordFragment(); 
          fm = activity.getFragmentManager(); 
          ft = fm.beginTransaction(); 
          ft.replace(R.id.layoutToReplace, 
            tempPasswordFragment); 
          ft.commit(); 
          String tempPassword = resultMap.get("tempPassword") 
            .toString(); // TODO: encrypt 
          String expiryDate = resultMap.get("expiryDate") 
            .toString(); 
//       now I need to update two TextViews in tempPasswordFragment based with these values 
         } 
          break; 
         } 
        } else { 
         // TODO: init UI with connect with GCH option 
         ConnectGCHFragment connectGCHFragment = new ConnectGCHFragment(); 
         fm = activity.getFragmentManager(); 
         ft = fm.beginTransaction(); 
         ft.replace(R.id.layoutToReplace, connectGCHFragment); 
         ft.commit(); 
        } 
       } 
      } 

     } catch (Exception e) { 
      Log.d(TAG, e.getMessage()); 
     } 
    } 
} 
+0

您可以從片段中調用任何方法,就像您從任何其他類的對象調用一樣。在片段中創建所需的方法,並在onPostExecute()方法中調用它。 –

+0

使用sharedPreference存儲值並在需要顯示時將其取回。 –

+0

試試這個可能會幫助你[鏈接](http://stackoverflow.com/a/35238587/2183890) – AKSiddique

回答

1

先放你如電子郵件,姓名等數據,在一個包和發送束作爲參數傳遞給目標片段,如下圖所示: -

  `Bundle bundle = new Bundle(); 
      bundle.putSerializable("email ", email); 
      bundle.putBoolean("name", name); 
      fragment.setArguments(bundle);` 

在目標片段獲得數據如下:

    `String email=getArguments().getString("email"); 
        String name=getArguments().getString("name");`