我如何从片段中的无线电组中获取值, 我有1个片段来显示问题,我想获得价值 活动的用户回答,因为活动中的下一个和上一步按钮是, 我的问题来自sqlite数据库
我的活动片段
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.view_pager);
setupViewPager(viewPager);
back = findViewById(R.id.btnBack);
next = findViewById(R.id.btnNext);
back.setOnClickListener(this);
next.setOnClickListener(this);
}
显示从框架到活动的问题,已使用ViewPager
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(QuestionFragment.newInstance(String.valueOf(0));, "1");
adapter.addFrag(QuestionFragment.newInstance(String.valueOf(1));, "2");
adapter.addFrag(QuestionFragment.newInstance(String.valueOf(2);, "3");
adapter.addFrag(QuestionFragment.newInstance(String.valueOf(3));, "4");
adapter.addFrag(QuestionFragment.newInstance(String.valueOf(4));, "5");
adapter.addFrag(QuestionFragment.newInstance(String.valueOf(5);, "6");
adapter.addFrag(QuestionFragment.newInstance(String.valueOf(6));, "7");
adapter.addFrag(QuestionFragment.newInstance(String.valueOf(7));, "8");
adapter.addFrag(QuestionFragment.newInstance(String.valueOf(8));, "9");
adapter.addFrag(QuestionFragment.newInstance(String.valueOf(9));, "10");
viewPager.setOffscreenPageLimit(adapter.getCount());
viewPager.setAdapter(adapter);
}
下一步和返回
按钮@Override
public void onClick(View view) {
if(view == back){
mfragmentIndex--;
viewPager.setCurrentItem(mfragmentIndex);
}else if(view == next) {
mfragmentIndex++;
viewPager.setCurrentItem(mfragmentIndex);
}
}
class View Pager
class ViewPagerAdapter extends FragmentPagerAdapter{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitle = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment,String title){
mFragmentList.add(fragment);
mFragmentTitle.add(title);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return super.getPageTitle(position);
}
}
我的片段显示问题
public static QuestionFragment newInstance(String questionId1) {
final QuestionFragment fragment = new QuestionFragment();
final Bundle args = new Bundle();
args.putString(ARG_QUESTION_ID,questionId1);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final String question1 = getArguments().getString(ARG_QUESTION_ID);
final DBAdapter dbAdapter = new DBAdapter(getContext());
questionsList = dbAdapter.getAllQuestions();
currentQuestion = questionsList.get(Integer.parseInt(question1));
View view = inflater.inflate(R.layout.fragment_question, container, false);
final RadioGroup grp=view.findViewById(R.id.radioGroup1);
txtQuestion=view.findViewById(R.id.tvQuestion);
rbtnA=view.findViewById(R.id.radio0);
rbtnB=view.findViewById(R.id.radio1);
rbtnC=view.findViewById(R.id.radio2);
rbtnD=view.findViewById(R.id.radio3);
setPertanyaan();
return view;
}
private void setPertanyaan()
{
txtQuestion.setText(currentQuestion.getQUESTION());
rbtnA.setText(currentQuestion.getOptionA());
rbtnB.setText(currentQuestion.getOptionB());
rbtnC.setText(currentQuestion.getOptionC());
rbtnD.setText(currentQuestion.getOptionD());
}
答案 0 :(得分:0)
这可以通过在fragment类中创建一个接口,然后在活动中实现它来完成。
在片段中创建一个接口,例如:-
// Container Activity must implement this interface
public interface OnRadioGroupSelectedListener {
public void onButtonSelected(String value);
}
全局声明片段类中的侦听器对象:-
OnRadioGroupSelectedListener mCallback;
Fragment在其onAttach()生命周期方法中捕获接口实现,然后可以调用Interface方法以与Activity通信。
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnRadioGroupSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnRadioGroupSelectedListener");
}
}
现在要接收回调,请添加:-
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
// if(checkedId==R.id.radio0) {
// mCallback.onButtonSelected(0)
// }else if (checkedId==R.id.radio1) {
// mCallback.onButtonSelected(1);
// }else if (checkedId==R.id.radio2) {
// mCallback.onButtonSelected(2);
// }else if (checkedId==R.id.radio3) {
// mCallback.onButtonSelected(3);
// }
RadioButton radioButton = group.findViewById(checkedId);
mCallback.onButtonSelected(radioButton.getText());
});
回调将选中的单选按钮的位置传递给活动。
现在在活动中实现列表器
implements QuestionFragment.OnRadioGroupSelectedListener
并重写方法以获取活动中选定按钮的位置。
注意:-您还可以根据需要更改onButtonSelected(String selectedRadioButtonValue)
参数。