我的应用程序的屏幕分为两个片段(上层和下层)。上部片段是一个问题(QuizFragmentType1),下部片段(QuizFragment)包含一个按钮“ next”,该按钮正在更改上部片段。

如何通过按下下部片段中的“下一个”按钮从上部片段中获取数据?

问题出在方法checkAnswers()中。当我尝试从QuizFragmentType1获取一些数据时,我无法。例如:

RadioGroup grp = (RadioGroup) getView().findViewById(R.id.radioGroup1);


radioGroup1位于QuizFragmentType1的布局中,所以我无法达到它。
我显然错过了这两个片段之间的一些交流。

有什么建议么?

这是代码:

较低的片段(QuizFragment.java)

public class QuizFragment extends BaseFragment implements View.OnClickListener {

List<Question> quesList;
Button butNext;
EditText input_answer;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
int score = 0;
int qid = 0;
private GameActivity activity;
int i = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.fragment_quiz, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);
    butNext = (Button) view.findViewById(R.id.button1);
    butNext.setOnClickListener(this);
    Random r = new Random();
    int Low = 1;
    int High = 3;
    int numb = r.nextInt(High - Low) + Low;

    if (numb == 1) {
        ((GameActivity) getActivity()).addFragment(R.id.game,
                new QuizFragmentType1());
    } else if (numb == 2) {
        ((GameActivity) getActivity()).addFragment(R.id.game,
                new QuizFragmentType2());
    }
}

public void randomQuestionType() {
    if (i < 5) {
        Random r = new Random();
        int Low = 1;
        int High = 3;
        int numb = r.nextInt(High - Low) + Low;

        if (numb == 1) {
            ((GameActivity) getActivity()).addFragment(R.id.game,
                    new QuizFragmentType1());
        } else if (numb == 2) {
            ((GameActivity) getActivity()).addFragment(R.id.game,
                    new QuizFragmentType2());
        }
        i++;
    }
    else {
        ((GameActivity) getActivity()).setupFragment(R.id.game,
                new ResultFragment());
    }

}

public void checkAnswers() {
    RadioGroup grp = (RadioGroup) getView().findViewById(R.id.radioGroup1);
    RadioButton answer = (RadioButton) getView().findViewById(
            grp.getCheckedRadioButtonId());

    String input = ((EditText) getView().findViewById(R.id.userInput))
            .getText().toString();

    if (currentQ.getAnswer().equals(answer.getText())) {
        score++;
        Log.d("score", "Your score" + score);

    } else if (currentQ.getAnswer().equals(input)) {
        score++;
        Log.d("score", "Your score" + score);
        input_answer.getText().clear();
    }
}

public void onClick(View v) {
    randomQuestionType();
}


}

上片段(QuizFragmentType1.java)或(QuizFragmentType2.java)

public class QuizFragmentType1 extends BaseFragment implements SetQuestionView {

static List<Question> quesList;
int qid = 0;
static Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;

float help = 0;
private GameActivity activity;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (this.activity == null)
        this.activity = (GameActivity) activity;
    quesList = Question.getQuestions(this.activity.getCurrentCategory());
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.fragment_quiz_type1, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);

    Random br = new Random();
    currentQ = quesList.get(br.nextInt(quesList.size()));

    txtQuestion = (TextView) view.findViewById(R.id.textView1);
    rda = (RadioButton) view.findViewById(R.id.radio0);
    rdb = (RadioButton) view.findViewById(R.id.radio1);
    rdc = (RadioButton) view.findViewById(R.id.radio2);

    setQuestionView();

    for (int i = 0; i < quesList.size(); i++) {
        Log.d("Debug", "Pitanje " + i + ": "
                + quesList.get(i).getQuestion());
    }
}
public void setQuestionView() {
    txtQuestion.setText(currentQ.getQuestion());
    rda.setText(currentQ.getOptA());
    rdb.setText(currentQ.getOptB());
    rdc.setText(currentQ.getOptC());
    qid++;
}


}

最佳答案

您唯一的方法是创建一个LocalBroadcastManager,该类可以处理应用程序所有组件(例如活动,片段,服务等)的通信(交互)。一旦Fragment已注册LocalBroadcastManager,它就会可以与您的自定义IntentFilter通信。您不需要在清单中注册它,只需在您需要从其他组件接收信息的类中注册它。

在发送者类中,调用:

Intent intent = new Intent("custom-event-name");
intent.putExtra("key", dataToBePassed);// may boolean, String, int, etc.
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);


在接收器中,将其注册到onCreate()方法中:

LocalBroadcastManager.getInstance(context).registerReceiver(mMessageReceiver,
      new IntentFilter("custom-event-name"));


另外,请确保您需要在onDestroy()方法中注销它:

LocalBroadcastManager.getInstance(context).unregisterReceiver(mMessageReceiver);


从接收方Fragment类获取信息:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String message = intent.getStringExtra("key");
        Log.d("receiver", "Got message: " + message);
      }
    };


注意:如果在context上注册,请用getActivity()更改Fragment。如果在Activity上,请用this进行更改。

有关更多示例,请参见this post

10-07 22:29