在我的Android应用中,我有两个片段。一个片段有一个onClickListener,基本上我要做的是创建一个计数器/日志。每次单击一个按钮,我想更新一个整数,然后将String.valueOf(整数)传递给另一个片段中的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(整数)的片段。
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(并在应用程序完全关闭后保留它)。
答案 0 :(得分:3)
您可以使用片段的父活动中的变量轻松传递数据。将该变量设为静态
public static Bundle myBundle = new Bundle();
现在将其从第一个片段更新为
YourParentActivityName.myBundle.putString("id_User", String.valueOf(COUNT));
现在在第二个片段中,您可以通过
获取此值String myValue = YourParentActivityName.myBundle.get("id_User");
答案 1 :(得分:1)
我认为你的" TextView将恢复原状状态的问题"是单击该按钮超时,您实例化一个新的TeaCounterFragment
。
在First Fragment上,创建一个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));
}
}
希望这能解决你的问题。
答案 2 :(得分:0)
您正在创建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);
另一个错误可能是您没有使用此创建的片段对象来呈现您的视图。 确保您使用相同的实例来显示视图。
答案 3 :(得分:0)
在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();
}