我在这里尝试了其他解决方案,但遗憾的是无法做到!我是Android新手,几乎完成了我的应用程序,但我被困在这里。
我想要做的就是点击Home Fragment中的 proceed 按钮,它会移动到下一个SGPA片段。
这就是您的代码现在的样子:
public class home extends Fragment {
//String Values For Courses and Semesters
String [] courses = {"BBA","BCA","B.Com Honours","B.Ed","B & I"};
String [] semesters = {"First", "Second", "Third", "Fourth", "Fifth", "Sixth"};
public home() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Course Spinner
View c = inflater.inflate(R.layout.home, container, false);
Spinner courseSpinner = (Spinner) c.findViewById(R.id.courses);
ArrayAdapter<String> LTRadapter = new ArrayAdapter<>(this.getActivity(), android.R.layout.simple_spinner_item, courses);
LTRadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
courseSpinner.setAdapter(LTRadapter);
//Semester Spinner
Spinner Spinner = (Spinner) c.findViewById(R.id.semester);
ArrayAdapter<String> LTRadapter1 = new ArrayAdapter<>(this.getActivity(), android.R.layout.simple_spinner_item, semesters);
LTRadapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
Spinner.setAdapter(LTRadapter1);
//Proceed Button
Button b = (Button) c.findViewById(R.id.proceedbtn);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = null;
fragment = new sgpa();
replaceFragment(fragment);
}
});
return c;
}
public void replaceFragment(Fragment someFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.sgpaFragment, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
答案 0 :(得分:1)
<强>解决方案强>
只要确保你给你的主片段一个id,所以你可以替换它,你在replaceFragment方法中有正确的想法,只是不做正确的交易!
public void replaceFragment(Fragment someFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.home, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}