我要疯了!!我无法将数据从片段(A)传递到另一个片段(B)。我阅读了有关使用公共接口的信息......它似乎可以工作,但我不明白如何使用这种方法。
片段(A)
package it.anddev.pagertabs;
public class Page1Fragment extends Fragment {
String Str;
OnDataPass dataPasser;
Class Senddata extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
// do something [...]
}
@Override
protected String doInBackground(String... args) {
// do something [...]
dataPasser.onDataPass(result_array.toString());
}
return null;
}
protected void onPostExecute(String file_url) {
// do something [...]
}};
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
dataPasser = (OnDataPass) activity;
}
public interface OnDataPass {
public void onDataPass(String data);
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// do something [...]
Button Avanti = (Button) view.findViewById(R.id.sendbutton);
Avanti.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//mViewPager.setCurrentItem(1, true);
new Senddata().execute();
}
});
片段B
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
if (container == null) {
return null;
}
View view = (LinearLayout)inflater.inflate(R.layout.page2,container,false);
Button mostra = (Button) view.findViewById(R.id.mostrabutton);
// String str = i need to get string from "public void onDataPass"
mostra.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//mViewPager.setCurrentItem(1, true);
Log.d("IT WORKS", str);
}
});
return view;
}
@Override
public void onDataPass(final String data) {
// TODO Auto-generated method stub
Log.d("okkkkkk", "" + data);
}
所以最后,我如何从fragmentB中的公共void获取字符串?谢谢