我在创建调用异步任务的活动。
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);
}
}
现在,异步任务的结果决定了在我能够做的活动中加载哪个片段。但无法想象如何更新片段内的textviews。请看一下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 :(得分:1)
首先将您获得的数据(例如电子邮件和名称)放在一个包中,然后将包作为参数发送到目标片段,如下所示: -
`Bundle bundle = new Bundle();
bundle.putSerializable("email ", email );
bundle.putBoolean("name", name);
fragment.setArguments(bundle);`
在目的地Fragment中获取如下数据:
`String email=getArguments().getString("email");
String name=getArguments().getString("name");`