我正在从蓝牙流中读取数据,当我读取的字符串是温度值时,我想将textview更新为该值。 问题是我从片段活动中读取数据,而我的textview在片段内。 任何人都可以帮助我吗?
这是我收集蓝牙数据的代码(实际收集被遗漏)
public class ListResult extends FragmentActivity implements SearchResult.OnSearchResultSelectedListener {
ProgressDialog pd;
RecipeInstructions instructions = new RecipeInstructions();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
...
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
byte[] writeBuf = (byte[]) msg.obj;
int begin = (int)msg.arg1;
int end = (int)msg.arg2;
String writeMessage;
switch(msg.what) {
case 1:
/*
check the bluetooth message, which is sent by the Arduino bluetooth module
depending on the message different tasks will be executed
the swipe gestures, the temperature sensor, the weigh scale
*/
writeMessage = new String(writeBuf);
writeMessage = writeMessage.substring(begin, end);
int position = pager.getCurrentItem();
Log.e("BLUETOOTH", writeMessage);
if(writeMessage.contains("RIGHT")) {
Log.e("SCROLL", "if function");
index = position -1;
scroll();
}
else if(writeMessage.contains("LEFT")) {
Log.e("SCROLL", "if function");
index = position +1;
scroll();
}
else if(writeMessage.contains("Temperature")) {
setTemp(writeMessage);
}
break;
}
}
};
}
public void setTemp(String message){
String value = message.substring(message.lastIndexOf(" ")+1, message.length());
instructions.setTemp(value + "°C");
}
这是我的片段
public class RecipeInstructions extends Fragment {
LinearLayout ll;
RelativeLayout ll1;
TextView textview;
TextView desc;
ImageView imgview;
ImageView temp;
TextView tempValue;
ScrollView scroll;
String temperature;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_ing_check, container, false);
...
tempValue = (TextView) v.findViewById(R.id.tempValue);
tempValue.setVisibility(View.VISIBLE);
new ImageDownloader(imgview).execute( createNewUrl(((ListResult) getActivity()).getUrl()));
return v;
}
public void setTemp(String temperature){
tempValue.setText(temperature);
Log.e("TEMP", "t= " + this.temperature);
}
答案 0 :(得分:0)
尝试使用
fragment= fragmentManager.findFragmentById (fragmentId);
或
fragment= fragmentManager.findFragmentByTag (FragmentTag);
现在您可以将数据设置为片段内的TextView。
((RecipeInstructions)fragment).setData(data);
在setData
Fragment
public class RecipeInstructions extends Fragment {
// ....
public void setData(String data){
textView.setText(data);
}
// .....