Как получить строку из каждой радиопереключателя, который проверяется после инфляции - PullRequest
0 голосов
/ 13 марта 2019

Мой код правильно раздувает макет, добавляя правильное количество переключателей и добавляя правильный текст ко всем из них.Что я не могу понять, так это как получить значение для каждого из отмеченных переключателей и Textview, когда оно является дочерним по отношению к линейному макету.Также я должен быть уверен, что строки из Textview и Radiobutton имеют одно и то же раздутое представление.Я не использую выбранный элемент, потому что мне нужно подождать, пока пользователь не убедится, что все ответы заполнены, прежде чем получать результаты.Так что у меня есть отдельная кнопка с onClick, которая собирается прочитать данные и сохранить их в общих настройках.

Вот мой код: Инфляция переходит в этот линейный макет "@ + id / linear_to_add": sp_button_check_yes_no.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
              android:background="@color/color_background_gray_dark"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <ScrollView
            android:layout_width="match_parent"
                android:layout_height="match_parent">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:id="@+id/linear_to_add"
                      android:layout_marginTop="20dp"
                      android:background="@color/color_background_gray_dark"
                      android:orientation="vertical"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"

        </LinearLayout>
    </ScrollView>
</LinearLayout>

Вот что каждый раз раздувается: barcode_options_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_marginBottom="20dp"
              android:layout_marginStart="10dp"
              android:layout_marginEnd="10dp"
              android:orientation="vertical"
              android:background="@drawable/button_style_gray"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

    <TextView
            android:id="@+id/textpop"
            android:textColor="@color/color_white"
            android:text="rdgbdrgkdrhguhkhuhu"
            android:paddingTop="6dp"
            android:paddingStart="40dp"
            android:textSize="21sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <RadioGroup
            android:id="@+id/radio_gr"
            android:layout_marginStart="30dp"
            android:paddingBottom="6dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

        <RadioButton
                android:id="@+id/rb1"
                android:visibility="visible"
                android:buttonTint="@color/color_white"
                android:textColor="@color/color_white"
                android:textSize="16sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="opt1"
               />

        <RadioButton
                android:id="@+id/rb2"
                android:visibility="visible"
                android:buttonTint="@color/color_white"
                android:textColor="@color/color_white"
                android:textSize="16sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="opt2" />

        <RadioButton
                android:id="@+id/rb3"
                android:visibility="visible"
                android:buttonTint="@color/color_white"
                android:textColor="@color/color_white"
                android:textSize="16sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="opt3"
        />

        <RadioButton
                android:id="@+id/rb4"
                android:visibility="visible"
                android:buttonTint="@color/color_white"
                android:textColor="@color/color_white"
                android:textSize="16sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="opt4" />

        <RadioButton
                android:id="@+id/rb5"
                android:visibility="visible"
                android:buttonTint="@color/color_white"
                android:textColor="@color/color_white"
                android:textSize="16sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="opt5" />

        <RadioButton
                android:id="@+id/rb6"
                android:visibility="visible"
                android:buttonTint="@color/color_white"
                android:textColor="@color/color_white"
                android:textSize="16sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="opt6" />

    </RadioGroup>

</LinearLayout>

Итак, я пытаюсь получить строку из каждой проверенной радиобаттонаи String формируют каждый TextView, раздутый в сторону с помощью радиокнопок.SpecialBarcodesActivity.java:

 private SharedPreferences mSharedPreferences;
private RadioGroup radio_gr;
private RadioButton rb1,rb2,rb3,rb4,rb5,rb6;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int screenHeight = getResources().getDisplayMetrics().heightPixels;
    int screenWidth  = getResources().getDisplayMetrics().widthPixels;

    mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    String barcodeAdditData = getIntent().getStringExtra("EXTRA_DATA");

    Toast.makeText(this, barcodeAdditData,
            Toast.LENGTH_LONG).show();


    String[] barcodeDataForPopupSeparated = barcodeAdditData.split("/");
    //reg_check/Q:How?;What;?/Op1:a;b;c;d;/Op2:1;2;3;4;



    if (barcodeDataForPopupSeparated[0].contains("reg_check"))
    {
        setContentView(R.layout.sb_check_yes_no);

        LayoutInflater mInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LinearLayout options_layout = (LinearLayout) findViewById(R.id.linear_to_add);

        String[] options1 = {"",""};
        String[] options2 = {"",""};
        String[] options3 = {"",""};
        String[] options4 = {"",""};
        String[] options5 = {"",""};
        String[] options6 = {"",""};

        barcodeDataForPopupSeparated[1] = barcodeDataForPopupSeparated[1].replace("Q:","");
        String[] question = barcodeDataForPopupSeparated[1].split(";");

        barcodeDataForPopupSeparated[2] = barcodeDataForPopupSeparated[2].replace("An1:","");
        options1 = barcodeDataForPopupSeparated[2].split(";");

        if(barcodeDataForPopupSeparated.length >= 4) {
            barcodeDataForPopupSeparated[3] = barcodeDataForPopupSeparated[3].replace("An2:", "");
            options2 = barcodeDataForPopupSeparated[3].split(";");
        }

        if(barcodeDataForPopupSeparated.length >= 5){
            barcodeDataForPopupSeparated[4] = barcodeDataForPopupSeparated[4].replace("An3:","");
            options3 = barcodeDataForPopupSeparated[4].split(";");
        }

        if(barcodeDataForPopupSeparated.length >= 6) {
            barcodeDataForPopupSeparated[5] = barcodeDataForPopupSeparated[5].replace("An4:", "");
            options4 = barcodeDataForPopupSeparated[5].split(";");
        }

        if(barcodeDataForPopupSeparated.length >= 7) {
            barcodeDataForPopupSeparated[6] = barcodeDataForPopupSeparated[6].replace("An5:", "");
            options5 = barcodeDataForPopupSeparated[6].split(";");
        }

        if(barcodeDataForPopupSeparated.length >= 8) {
            barcodeDataForPopupSeparated[7] = barcodeDataForPopupSeparated[7].replace("An6:", "");
            options6 = barcodeDataForPopupSeparated[7].split(";");
        }

        for (int i = 0; i < question.length; i++) {

            View to_add = mInflater.inflate(R.layout.barcode_options_item, options_layout,false);


            TextView text = (TextView) to_add.findViewById(R.id.textpop);
            text.setText(question[i]);


             radio_gr = (RadioGroup) to_add.findViewById(R.id.radio_gr);
             //radio_gr.setTag("0");

             rb1 = (RadioButton) to_add.findViewById(R.id.rb1);
             rb2 = (RadioButton) to_add.findViewById(R.id.rb2);
             rb3 = (RadioButton) to_add.findViewById(R.id.rb3);
             rb4 = (RadioButton) to_add.findViewById(R.id.rb4);
             rb5 = (RadioButton) to_add.findViewById(R.id.rb5);
             rb6 = (RadioButton) to_add.findViewById(R.id.rb6);


            if(i == 0) {
                addAnswers(options1);

            }else if(i == 1) {
                addAnswers(options2);
            }else if(i == 2) {
                addAnswers(options3);
            }else if(i == 3) {
                addAnswers(options4);
            }else if(i == 4) {
                addAnswers(options5);
            }else if(i == 5) {
                addAnswers(options6);
            }

            options_layout.addView(to_add);

            }
        }
    }


         for (int i = 0; i < question.length; i++) {

                            View to_add = mInflater.inflate(R.layout.barcode_options_item, options_layout,false);

                    //get String from every inflated TextView 
                            TextView text = (TextView) to_add.findViewById(R.id.textpop);
                            text.setText(question[i]);

            //get String from every inflated checked RadioButton
                             radio_gr = (RadioGroup) to_add.findViewById(R.id.radio_gr);

                             rb1 = (RadioButton) to_add.findViewById(R.id.rb1);
                             rb2 = (RadioButton) to_add.findViewById(R.id.rb2);
                             rb3 = (RadioButton) to_add.findViewById(R.id.rb3);
                             rb4 = (RadioButton) to_add.findViewById(R.id.rb4);
                             rb5 = (RadioButton) to_add.findViewById(R.id.rb5);
                             rb6 = (RadioButton) to_add.findViewById(R.id.rb6);

                            if(i == 0) {
                                addAnswers(options1);

                            }else if(i == 1) {
                                addAnswers(options2);
                            }else if(i == 2) {
                                addAnswers(options3);
                            }else if(i == 3) {
                                addAnswers(options4);
                            }else if(i == 4) {
                                addAnswers(options5);
                            }else if(i == 5) {
                                addAnswers(options6);
                            }
                            options_layout.addView(to_add);
                            }

 private void addAnswers(String[] optionNumb){

         int a;

         for (a = 0; a < optionNumb.length; a++) {

             if(a==0) {
                 rb1.setText(optionNumb[a]);
                 //rb1.setTag("rb1_"+a);
             }else if(a==1){
                 rb2.setText(optionNumb[a]);
                 //rb2.setTag("rb2_"+a);
             }else if(a==2){
                 rb3.setText(optionNumb[a]);
                 //rb3.setTag("rb3_"+a);
             }else if(a==3){
                 rb4.setText(optionNumb[a]);
                 //rb4.setTag("rb4_"+a);
             }else if(a==4){
                 rb5.setText(optionNumb[a]);
                 //rb5.setTag("rb5_"+a);
             }else if(a==5){
                 rb6.setText(optionNumb[a]);
                 //rb6.setTag("rb6_"+a);
             }

             if(optionNumb.length < 3){
                 rb3.setVisibility(View.GONE);
                 rb4.setVisibility(View.GONE);
                 rb5.setVisibility(View.GONE);
                 rb6.setVisibility(View.GONE);
             }else if(optionNumb.length < 4){
                 rb4.setVisibility(View.GONE);
                 rb5.setVisibility(View.GONE);
                 rb6.setVisibility(View.GONE);
             }else if(optionNumb.length < 5){
                 rb5.setVisibility(View.GONE);
                 rb6.setVisibility(View.GONE);
             }else if(optionNumb.length < 6){
                 rb6.setVisibility(View.GONE);
             }


         }
     }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...