【发布时间】:2015-12-15 13:13:34
【问题描述】:
我有一个片段,我在其中调用 asynctask 来获取一些数据,然后更新一些文本视图。
问题是当我第一次访问这个片段时,整个过程运行良好。如果我更改为另一个片段然后返回,则不会通过 settext 更新文本视图,但如果我在更新后调用 gettext,它会返回应该显示的值。
我在片段的 onCreateView 中初始化文本视图,并在 asynctask 的 onPostExecute 中更新它们。
我希望我解释得很好..有什么想法可能是问题吗?
onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_medical, container, false);
mContext = (FragmentActivity)getActivity();
current_meas_data = (TextView) v.findViewById(R.id.current_measurement_data);
bt = new MedicalBluetooth(mContext);
return v;
}
开始:
public void onStart() {
super.onStart();
boolean btavailable = bt.isBluetoothAvailable();
if (btavailable == false) {
Toast.makeText(mContext, "Bluetooth is not available",
Toast.LENGTH_LONG).show();
return;
}
boolean btenabled = bt.isBluetoothEnabled();
if (btenabled) {
bt.setupService();
bt.startService();
} else {
Intent enableIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, 1);
}
bt.setOnDataReceivedListener(new MedicalBluetooth.OnDataReceivedListener() {
public void onDataReceived(final byte[] data, String message) {
new GetMeasurementTask().execute(data);
}
});
}
异步任务:
private class GetMeasurementTask extends AsyncTask<byte[], Integer, Boolean> {
private ProgressDialog dialog = new ProgressDialog(mContext);
protected void onPreExecute() {
this.dialog.setMessage("Please wait");
this.dialog.show();
}
@Override
protected Boolean doInBackground(byte[]... params) {
com.example.bluetoothlibrary.Measurement mes = new com.example.bluetoothlibrary.Measurement();
mes = bt.manageData(params[0]);
return true;
}
protected void onPostExecute(Boolean result) {
if (dialog.isShowing()) {
dialog.dismiss();
}
if (result == true) {
//Toast.makeText(mContext, "Call Successful!", Toast.LENGTH_LONG).show();
Log.d("BEFORE SETTEXT",current_meas_data.getText().toString());
current_meas_data.setText(mes.data);
Log.d("AFTER SETTEXT",current_meas_data.getText().toString());
}
if (result == false) {
//Toast.makeText(mContext, "Call Error", Toast.LENGTH_LONG).show();
}
}
}
日志说:
设置文本之前:
设置文本后:20
但是“current_meas_data”是空的。
【问题讨论】:
-
更新了我的问题..
标签: android android-fragments android-asynctask textview fragment