通过动态创建的Edittext和RadioButtons将信息保存在Firebase数据库中

时间:2019-07-18 06:35:11

标签: android firebase-realtime-database

我用Firebase Realtimedatabase创建了调查应用程序。我已将问题存储在数据库中,并通过动态创建RadioButtons,Textview和Edittext在应用程序中获取问题。 现在我必须将用户填写的信息保存回Firebase数据库中。我进行搜索并了解了使用setTags的知识,但是在设置标签后如何真正地理解如何分隔edittext和单选按钮并在将它们推入Firebase时将它们分别取回我想从选中的每个edittext和单选按钮中获取数据,并将它们唯一地保存回firebase数据库中

这是Iam动态创建视图并在其中获取数据的代码

    if (getIntent().hasExtra(Constants.ref_no)) {
        surveyRefNo = getIntent().getStringExtra(Constants.ref_no);
    }
    if (getIntent().hasExtra(Constants.refChild)) {
        surveyRefChild = getIntent().getStringExtra(Constants.refChild);
    }

    databaseReference = FirebaseDatabase.getInstance().getReference().child(Constants.content).child(Constants.survey).child(surveyRefNo).child(surveyRefChild);
    System.out.println(databaseReference);
    System.out.println(surveyRefNo);
    linearLayout = findViewById(R.id.survey_question_linearlayout);

    databaseReference.child(Constants.questions).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                System.out.println(ds);

                if (ds.hasChildren() && ds.hasChild("title") && ds.hasChild("type") && ds.hasChild("options")) {

                    for (DataSnapshot option : ds.child("options").getChildren()) {
                        values.add(String.valueOf(option.getValue()));
                        System.out.println(values);
                        System.out.println(values.size());
                    }
                    String user = String.valueOf(ds.child("type").getValue());
                    title = (String) ds.child("title").getValue();
                    switch (Integer.parseInt(user)) {
                        case (1):
                            if (user != null) {
                                addRadioButtons();
                                values.clear();
                            }
                            break;
                        case (2):
                            if (user != null) {
                                questionView();
                            }
                            break;
                        default:
                            Toast.makeText(GetSurveys.this, "Sorry!!! Something went wrong", Toast.LENGTH_SHORT).show();
                    }
                } else if (ds.hasChildren() && ds.hasChild("title") && ds.hasChild("type")) {
                    String user = String.valueOf(ds.child("type").getValue());
                    title = (String) ds.child("title").getValue();
                    switch (Integer.parseInt(user)) {

                        case (1):
                            if (user != null) {
                                addRadioButtons();
                            }
                            break;
                        case (2):
                            if (user != null) {
                                questionView();
                            }
                            break;
                        default:
                            Toast.makeText(GetSurveys.this, "Sorry!!! Something went wrong", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });
}

public void addRadioButtons() {
    CardView cardView = new CardView(this);
    LinearLayout.LayoutParams cardParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    cardParams.setMargins(10, 10, 10, 10);
    cardView.setLayoutParams(cardParams);
    cardView.setCardBackgroundColor(Color.parseColor("#f8f8ff"));
    LinearLayout cardLayout = new LinearLayout(this);
    cardLayout.setOrientation(LinearLayout.VERTICAL);
    cardView.addView(cardLayout);
    linearLayout.addView(cardView);

    TextView radioText = new TextView(this);
    radioText.setText(title);

    LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    textParams.setMargins(10, 10, 10, 10);
    radioText.setLayoutParams(textParams);
    cardLayout.addView(radioText);

    RadioGroup radioGroup = new RadioGroup(this);

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    params.setMargins(10, 10, 10, 10);
    radioGroup.setLayoutParams(params);
    radioGroup.setOrientation(LinearLayout.VERTICAL);

    for (int index = 0; index < values.size(); index++) {
        RadioButton radioButton = new RadioButton(this);
        radioButton.setId(View.generateViewId());
        radioButton.setText(values.get(index));
        radioGroup.addView(radioButton);
    }
    cardLayout.addView(radioGroup);
}

public void questionView() {
    CardView cardView = new CardView(this);

    LinearLayout.LayoutParams cardParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    cardParams.setMargins(10, 10, 10, 10);
    cardView.setLayoutParams(cardParams);
    cardView.setCardBackgroundColor(Color.parseColor("#f8f8ff"));
    LinearLayout cardLayout = new LinearLayout(this);
    cardLayout.setOrientation(LinearLayout.VERTICAL);

    cardView.addView(cardLayout);
    linearLayout.addView(cardView);

    TextView questionText = new TextView(this);
    LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    textParams.setMargins(20, 10, 20, 10);
    questionText.setPadding(15, 10, 10, 10);
    questionText.setText(title);
    questionText.setGravity(16);
    questionText.setTextSize(17.0f);
    questionText.setLayoutParams(textParams);
    cardLayout.addView(questionText);
    EditText answerText = new EditText(this);
    LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    editParams.setMargins(20, 5, 20, 5);
    answerText.setPadding(15, 10, 10, 10);
    answerText.setHint("Your Answer");
    answerText.setTextSize(17.0f);
    answerText.setLayoutParams(editParams);
    cardLayout.addView(answerText);
}

}

0 个答案:

没有答案