在子片段到父片段或活动之间传递数据

时间:2019-03-25 09:27:32

标签: android android-fragments fragment

我有2个片段,一个是我的父片段,我有一个孩子片段,我想将两个片段数据都传递给活动,我已经声明了该通信的接口,但是  每当我在片段中分配接口方法值时遇到问题,都会得到空响应,因为我在 在create()上。 单击活动按钮时,我已发送多个编辑文本值和微调器值的日期。

    public class AddSubQuestion extends Fragment implements AdapterView.OnItemSelectedListener {

    private TextView addNewQuestxt;
    private AppCompatSpinner choiceSpinner;
    private String[] choiceList = {"Single Choice", "Multiple Choice", "Multi-Line Text"};
    private RelativeLayout relativeSpinner, relativeLayoutSingleChoice, relativeLayoutMultipleChoice, relativeLayoutMultiLinesChoice;
    private EditText questionTxt1, answerTxt1, answerTxt1_multiple, answerTxt2_multiple, answerTxt3_multiple, answerTxt4_multiple, answerTxt1_multiline;
    private TextView addNewSubSingle, addNewSubMultiple, addNewSubMultiline;
    private String questTxt, ansSingleTxt, ansMultipleTxt1, ansMultipleTxt2, ansMultipleTxt3, ansMultipleTxt4, ansMultilineTxt1;
    private static final String TAG = AddSubQuestion.class.getSimpleName();

    /* public static Object newInstance() {
      return null;
  }
*/
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public AddSubQuestion() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.activity_add_sub_new_question, container, false);

        addNewQuestxt = view.findViewById(R.id.plus_btn);

        relativeSpinner = view.findViewById(R.id.spinner_relative);
        relativeLayoutSingleChoice = view.findViewById(R.id.singlechoice_relative);
        relativeLayoutMultipleChoice = view.findViewById(R.id.multiplechoice_relative);
        relativeLayoutMultiLinesChoice = view.findViewById(R.id.multilinechoice_relative);

        questionTxt1 = view.findViewById(R.id.question_edit1);

        //single choice
        answerTxt1 = view.findViewById(R.id.answer_edit1);

        addNewSubSingle = view.findViewById(R.id.plus_btn_singlechoice);

        //multiple choice
        answerTxt1_multiple = view.findViewById(R.id.answer_edit11);
        answerTxt2_multiple = view.findViewById(R.id.answer_edit22);
        answerTxt3_multiple = view.findViewById(R.id.answer_edit33);
        answerTxt4_multiple = view.findViewById(R.id.answer_edit44);

        addNewSubMultiple = view.findViewById(R.id.plus_btn_multiple);

        //mutilines choice
        answerTxt1_multiline = view.findViewById(R.id.answer_edit111);
        addNewSubMultiline = view.findViewById(R.id.plus_btn_multiline);


        choiceSpinner = view.findViewById(R.id.choice_spinner);

        choiceSpinner.setOnItemSelectedListener(this);

        //Creating the ArrayAdapter instance having the country list
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_spinner_item, choiceList);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //Setting the ArrayAdapter data on the Spinner
        choiceSpinner.setAdapter(spinnerAdapter);

        return view;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getActivity(), choiceList[position], Toast.LENGTH_LONG).show();
        if (choiceList[position].equalsIgnoreCase("Multiple Choice")) {
            Log.d(TAG, "onItemSelected:  multiple choice :");
            relativeLayoutSingleChoice.setVisibility(View.GONE);
            relativeLayoutMultiLinesChoice.setVisibility(View.GONE);
            relativeLayoutMultipleChoice.setVisibility(View.VISIBLE);
        } else if (choiceList[position].equalsIgnoreCase("Multi-Line Text")) {
            Log.d(TAG, "onItemSelected:  multi line text");
            relativeLayoutMultipleChoice.setVisibility(View.GONE);
            relativeLayoutSingleChoice.setVisibility(View.GONE);
            relativeLayoutMultiLinesChoice.setVisibility(View.VISIBLE);
        } else {
            relativeLayoutSingleChoice.setVisibility(View.VISIBLE);
            relativeLayoutMultiLinesChoice.setVisibility(View.GONE);
            relativeLayoutMultipleChoice.setVisibility(View.GONE);
        }
    }


    public MultipleChoiceQuesData getMultipleText() {
         MultipleChoiceQuesData questionData = new MultipleChoiceQuesData();

        questionData.setChoice(choiceSpinner.getSelectedItem().toString());
        questionData.setQuestion(questionTxt1.getText().toString());
        questionData.setAnswer(answerTxt1_multiple.getText().toString().trim());
        questionData.setAnswer2(answerTxt2_multiple.getText().toString().trim());
        questionData.setAnswer3(answerTxt3_multiple.getText().toString().trim());
        questionData.setAnswer4(answerTxt4_multiple.getText().toString().trim());
        //return questionData;

        return questionData;
    }

    public String getChoice() {
        return choiceSpinner.getSelectedItem().toString();
    }

    public MultilinesChoiceQueData getMultilinesText() {
        MultilinesChoiceQueData multilinesChoiceQueData = new MultilinesChoiceQueData();

        multilinesChoiceQueData.setChoice(choiceSpinner.getSelectedItem().toString());
        multilinesChoiceQueData.setQuestion(questionTxt1.getText().toString().trim());
        multilinesChoiceQueData.setAnswer(answerTxt1_multiple.getText().toString().trim());

        return multilinesChoiceQueData;
    }

    public SingleChoiceQuesData getSingleChoiceText() {
        SingleChoiceQuesData singleChoiceQuesData = new SingleChoiceQuesData();

        singleChoiceQuesData.setChoice(choiceSpinner.getSelectedItem().toString());
        singleChoiceQuesData.setQuestion(questionTxt1.getText().toString().trim());
        singleChoiceQuesData.setAnswer(answerTxt1.getText().toString().trim());

        return singleChoiceQuesData;
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
    public interface OnChildDataPass {
        public void onChildDataPass(String data);
    }
}

这是我的孩子片段

0 个答案:

没有答案