我有一个选项卡式活动,该活动使用SectionsPagerAdapter来管理片段。如何发送字符串到片段?
我有一个使用SectionsPagerAdapter的选项卡式活动。 MainActivity控制用于从Bluetooth接收数据的handle方法。我需要将这些数据传递给我正在使用的3个片段。 我已经尝试过bundle参数,但是Fragment中什么也没发生。
这是我的MainActivity onCreate
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
final ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
//CONEXION BT
Intent intent = getIntent();
MACdireccion = intent.getStringExtra("EXTRA_ADDRESS");
new ConnectBT().execute();
btIn = new Handler(){
public void handleMessage(Message msg){
if(msg.what == handlerState){
String readMsg = (String) msg.obj;
recDataString.append(readMsg);
int endOfLineIndex = recDataString.indexOf("#");
if(endOfLineIndex > 0){
String dataIn = recDataString.substring(0,endOfLineIndex);
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
hum = dataIn.substring(dataIn.indexOf("&")+1,dataIn.indexOf("H"));
temp = dataIn.substring(dataIn.indexOf("H")+1,dataIn.indexOf("C"));
DA = dataIn.substring(dataIn.indexOf("C")+1,dataIn.indexOf("A"));
DB = dataIn.substring(dataIn.indexOf("A")+1,dataIn.indexOf("B"));
DateTime = dateFormat.format(date);
}
}
sectionsPagerAdapter.Data(DA, DB, hum, temp, DateTime);
//viewPager.getAdapter().notifyDataSetChanged();
}
};
这是我的SectionPagerAdapter
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position){
case 0:
fragment = new Frag_GENERAL().newInstance(DA, DB, String.valueOf(DP), hum, temp);
fragment.setArguments(bundleDA); fragment.setArguments(bundleDB);
fragment.setArguments(bundleDP); fragment.setArguments(bundleHUM);
fragment.setArguments(bundleTEMP);
((Frag_GENERAL) fragment).upDate();
return fragment;
//((Frag_GENERAL) fragment).DatosSonda();
case 1:
fragment = new Frag_GRAFICA();
return fragment;
case 2:
fragment = new Frag_TXT();
return fragment;
}
return null;
}
@Override
public int getItemPosition(@NonNull Object object) {
return POSITION_NONE;
}
public void Data(String A, String B, String H, String T, String dt){
DA = A; DB = B; hum = H;
temp = T; DateTime = dt;
DP = 0.000000152;
//DP = (Double.valueOf(DA)+Double.valueOf(DB))/2.00;
escala = "mSv/h";
if(DP < 1){
DP = DP / 1000;
escala = "uSv/h";
if(DP < 1){
DP = DP / 1000;
escala = "nSv/h";
}
}
bundleDA.putString("ARG_DA", "hola");
bundleDB.putString("ARG_DB", "hola");
bundleDP.putString("ARG_DP", "hola".toString());
bundleHUM.putString("ARG_HUM", "hola");
bundleTEMP.putString("ARG_TEMP", "hola");
}
这是我的片段
public static Frag_GENERAL newInstance(String DosisA, String DosisB, String DosisP, String humedad, String temperatura) {
Frag_GENERAL fragment = new Frag_GENERAL();
Bundle args = new Bundle();
args.putString(ARG_DA, DosisA);
args.putString(ARG_DB, DosisB);
args.putString(ARG_DP, DosisP);
args.putString(ARG_HUM, humedad);
args.putString(ARG_TEMP, temperatura);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
DA = getArguments().getString(ARG_DA);
DB = getArguments().getString(ARG_DB);
DP = getArguments().getString(ARG_DP);
HUM = getArguments().getString(ARG_HUM);
TEMP = getArguments().getString(ARG_TEMP);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.frag_general, container, false);
textDA = (TextView) root.findViewById(R.id.tv_DA);
textDB = (TextView) root.findViewById(R.id.tv_DB);
textDP = (TextView) root.findViewById(R.id.tv_DP);
textHUM = (TextView) root.findViewById(R.id.tv_hum);
textTEMP = (TextView) root.findViewById(R.id.tv_temp);
textLATLONG = (TextView) root.findViewById(R.id.tv_latLONG);
textDA.setText(DA);
textDB.setText(DB);
textDP.setText(DP);
textHUM.setText(HUM);
textTEMP.setText(TEMP);
textLATLONG.setText("Lat: " + 14.00 + "Long: " + 15.00);
return root}