[英]Pass Data from Fragment to Fragment Through Button Click
在我的Android應用中,我有兩個片段。 一個片段有一個onClickListener,基本上我想做的是創建各種計數器/日志。 每次單擊按鈕時,我要更新一個整數,然后將String.valueOf(integer)傳遞給另一個片段中的TextView。
這是第一個片段,帶有onClickListener:
public class StartingFragment extends Fragment implements View.OnClickListener {
public static final String TEA_TYPE_POS = "tea_navdrawer_position";
public static int COUNT = 0;
private TeaCounterFragment mTeaCounterFragment;
// onCreateView method - Returning the layout file
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflating the layout
View v = inflater.inflate(R.layout.starting_fragment, container, false);
/* From this point, you do everything in regards to the "v" object */
Button tea_type1 = (Button) v.findViewById(R.id.tea_type1);
Button tea_type2 = (Button) v.findViewById(R.id.tea_type2);
Button tea_type3 = (Button) v.findViewById(R.id.tea_type3);
Button tea_type4 = (Button) v.findViewById(R.id.tea_type4);
Button tea_type5 = (Button) v.findViewById(R.id.tea_type5);
Button tea_type6 = (Button) v.findViewById(R.id.tea_type6);
Button tea_type7 = (Button) v.findViewById(R.id.tea_type7);
Button set_timer = (Button) v.findViewById(R.id.set_your_own_timer);
tea_type1.setText("Oolong");
tea_type1.setOnClickListener(this);
tea_type2.setText("White");
tea_type2.setOnClickListener(this);
tea_type3.setText("Blooming");
tea_type3.setOnClickListener(this);
tea_type4.setText("Black");
tea_type4.setOnClickListener(this);
tea_type5.setText("Herbal");
tea_type5.setOnClickListener(this);
tea_type6.setText("Green");
tea_type6.setOnClickListener(this);
tea_type7.setText("Mate");
tea_type7.setOnClickListener(this);
set_timer.setText("Set Your Own Timer");
set_timer.setOnClickListener(this);
/* Do your manipulation to your views here, onClick listeners and such */
// Return the "v" object
return v;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
/*
* Use the View interface with OnClickListener to get the Button ID's
* Then you can run a switch on the Buttons (because normally switches
* cannot be run on buttons
*/
case R.id.tea_type1:
Builder oolongBuilder = new AlertDialog.Builder(StartingFragment.this.getActivity(),
AlertDialog.THEME_HOLO_LIGHT);
oolongBuilder.setPositiveButton("Hot",
//Starts OolongTeaActivity for hot tea when clicked
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Intent i = new Intent(StartingFragment.this.getActivity(),
OolongTeaActivity.class);
StartingFragment.this.getActivity().startActivity(i);
}
});
oolongBuilder.setNeutralButton("Iced",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(StartingFragment.this.getActivity(),
ColdOolongTeaActivity.class);
StartingFragment.this.getActivity().startActivity(i);
}
});
oolongBuilder.setTitle("Oolong Tea");
oolongBuilder.setMessage("How Do You Like Your Tea?");
AlertDialog oolongDialog = oolongBuilder.create();
oolongDialog.show();
COUNT++;
Fragment fragment = new Fragment();
final Bundle bundle = new Bundle();
bundle.putString("id_User", String.valueOf(COUNT));
Log.i("BUNDLE", bundle.toString());
fragment.setArguments(bundle);
break;
還有我希望valueOf(integer)進入的片段。
public class TeaCounterFragment extends Fragment {
public TeaCounterFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tea_counter, container, false);
TextView oolongCounterText = (TextView) rootView.findViewById(R.id.counter_tv);
Bundle args = getArguments();
if (args != null && args.containsKey("id_User")){
String userId = args.getString("id_User");
oolongCounterText.setText(userId);
}
return rootView;
}
我已經意識到TextView將恢復為原始狀態,但是如果至少在單擊按鈕后可以更新它,那么我可以弄清楚如何在以后永久保存它。
我已經看過了Android的開發者文檔,它並說,兩個片段不應該直接溝通,但我不明白為什么我現在使用的方法不應該工作。
編輯:嘗試解決此問題的另一種方法,但我得到了NullPointerException。 我決定在一個Fragment中創建一個接口,並通過NavDrawer(MainActivity)類嘗試更新TextView。
pastebin.com/1dx5rEVv(MainActivity)--- pastebin.com/7wKW1zq1(StartingFragment )
在這一點上,我只想使用一種方法或一種尚未使用的方法來更新TextView(甚至在應用程序完全關閉后也要保留它)。
您可以通過使用片段父活動中的變量輕松地傳遞數據。 將該變量設為靜態
public static Bundle myBundle = new Bundle();
現在從第一個片段更新為
YourParentActivityName.myBundle.putString("id_User", String.valueOf(COUNT));
現在在第二個片段中,您可以通過
String myValue = YourParentActivityName.myBundle.get("id_User");
我認為您的“ TextView將恢復為原始狀態”的問題是,單擊超時按鈕后,您實例化了一個新的TeaCounterFragment
。
在您的第一個片段上,創建一個TeaCounterFragment並在onCreate函數上實例化它。
public class YourFirstFragment extends Fragment {
private TeaCounterFragment mTeaCounterFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
mTeaCounterFragment = new TeaCounterFragment();
getFragmentManager().beginTransaction()
.add(R.id.container, mTeaCounterFragment)
.commit();
}
}
}
在第一個片段的onClick上,只需將其添加到TeaCounterFragment上所需的更新中即可。
@Override
public void onClick(View view) {
...
COUNT++;
mTeaCounterFragment.UpdateCount(COUNT);
...
}
在TeaCounterFragment上,創建一個公共函數來更新您的UI並使用此函數修改onCreateView。
public class TeaCounterFragment extends Fragment {
private TextView mTeaCounterText;
public TeaCounterFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tea_counter, container, false);
mTeaCounterText = (TextView) rootView.findViewById(R.id.counter_tv);
return rootView;
}
public void UpdateCount(int count)
{
mTeaCounterText.setText(String.valueOf(count));
}
}
希望這能解決您的問題。
您正在創建一個Fragment Class對象。 您需要創建TeaCounterFragment類。
TeaCounterFragment fragment = new TeaCounterFragment ();
final Bundle bundle = new Bundle();
bundle.putString("id_User", String.valueOf(COUNT));
Log.i("BUNDLE", bundle.toString());
fragment.setArguments(bundle);
另一個錯誤可能是您沒有使用此創建的片段對象來呈現視圖。 確保使用同一實例來呈現視圖。
在Fragment類中定義接口之后,還需要確保使用活動在片段中使用onAttach
方法來實現該接口,如下所示:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener = (updateFragment) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement the interface");
}
}
然后,要從您的活動中更新片段,請執行以下操作:
@Override
public void onButtonClick(String message) {
TeaCounterFragment fragment = new TeaCounterFragment();
final Bundle bundle = new Bundle();
bundle.putString("id_User", String.valueOf(COUNT));
Log.i("BUNDLE", bundle.toString());
fragment.setArguments(bundle);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_replace, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.