Java 通过单击按钮将数据从一个片段传递到另一个片段
在我的Android应用程序中,我有两个片段。一个片段有一个onClickListener,本质上我正在尝试创建一个计数器/排序日志。每次单击按钮时,我都要更新一个整数,然后将String.valueOfinteger传递给另一个片段中的TextView 这是第一个片段,带有onClickListener:Java 通过单击按钮将数据从一个片段传递到另一个片段,java,android,android-fragments,buttonclick,pass-data,Java,Android,Android Fragments,Buttonclick,Pass Data,在我的Android应用程序中,我有两个片段。一个片段有一个onClickListener,本质上我正在尝试创建一个计数器/排序日志。每次单击按钮时,我都要更新一个整数,然后将String.valueOfinteger传递给另一个片段中的TextView 这是第一个片段,带有onClickListener: public class StartingFragment extends Fragment implements View.OnClickListener { publ
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;
以及我想要积分的值所指向的片段
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。我决定在一个片段中创建一个接口,并通过NavDrawer MainActivity类尝试更新TextView
主活动-启动片段
此时,我只想更新TextView并保留它,即使应用程序完全关闭,也可以使用任何一种方法,或者使用尚未使用的方法。您正在创建Fragment类的对象。 您需要创建一个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);
另一个错误可能是您没有使用这个创建的片段对象来表示您的视图。
确保您使用的是同一个实例来显示视图。我认为TextView将恢复到其原始状态的问题在于,当单击按钮时,您将实例化一个新的TeaCounterFragment 在第一个片段上,创建一个teafersfragment并在onCreate函数上实例化它 公共类YourFirstFragment扩展了片段{ 私人茶具;反碎片;反碎片; @凌驾 受保护的void onCreateBundle savedInstanceState{ super.onCreatesavedInstanceState; 如果savedInstanceState==null{ mTeaCounterFragment=新的TeaCounterFragment; getFragmentManager.beginTransaction .addR.id.container,mTeaCounterFragment 犯罪 } } } 在单击第一个片段时,只需在TeaCounterFragment上添加所需的更新 @凌驾 公共void onclick视图{ ... 计数++; mTeaCounterFragment.UpdateCountCountCount; ... } 在TeaCounterFragment上,创建一个公共函数来更新UI,并使用此函数修改onCreateView 公共类TeaCounterFragment扩展了片段{ 私有文本查看多行计数器文本; 公共茶具{ } @凌驾 公共视图OnCreateViewLayoutFlater充气机、视图组容器、, 捆绑存储状态{ 视图根视图=充气机。充气机。布局。碎片_茶_计数器,容器,错误; mTeaCounterText=TextView rootView.findViewByIdR.id.counter\u tv; 返回rootView; } 公共无效更新计算计数 { mTeaCounterText.setTextString.valueOfcount; } }
希望这能解决您的问题。您可以通过在片段的父活动中使用变量轻松地传递数据。将该变量设为静态变量
public static Bundle myBundle = new Bundle();
现在将它从第一个片段更新为
YourParentActivityName.myBundle.putString("id_User", String.valueOf(COUNT));
现在在第二个片段中,您可以通过
String myValue = YourParentActivityName.myBundle.get("id_User");
在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();
}
这里我有点困惑,因为我有两个片段,所以没有onCreate方法,只有oncreateview默认情况下,在创建的片段上没有onCreate。您可以简单地重写该函数。确定。我试过你的方法,它确实有效。然而,有一个问题。我的TeaCounterFragment是它自己的片段,当我单击navdrawer中的第二项时,它会显示出来。My StartingFragment是默认情况下在我的应用程序中显示的片段,也是在我单击navdrawer中的第一项时显示的片段。NavigationDrawer.java是我的片段的父活动。在您给我的onCreate代码中,它最终同时显示StartingFragment和TeaCounterFragment,并且没有更新在单击我的导航抽屉中的第二项后显示的TeaCounterFragment。因此,我建议在您的主要活动中添加TeaCounterFragment,而不是在第一个片段上。这样,您就可以在活动上实现和接口侦听器。如果在不同的片段中创建同一个实例,我将如何使用它?如果我在另一个名为StartingFragment的片段中实例化一个新的TeaCounterFragment对象,我如何使用TeaCounterFragment类中的同一个实例
了解这些碎片。好啊我理解片段,只是我不完全确定如何处理它+心理障碍太好了!,希望这个答案对你有帮助…:,我无法使用这种方法使其工作……实际上,我对如何使用FragmentManager解决这个问题感到困惑。不过,我确实尝试了其他方法,但得到了一个NullPointerException MainActivity-StartingFragment