Как сохранить данные из нескольких Edittexts, которые добавляются динамически в Android? - PullRequest
0 голосов
/ 09 октября 2019

У меня некоторые EditTexts , и я пытаюсь сохранить их значения в ArrayList , но мои EditTexts добавляютсяс помощью кнопки добавления, которая добавляет LinearLayout , и EditTexts находятся в пределах создаваемого LinearLayouts.

Обычно с фиксированным количеством EditTexts было бы легко сохранить, но как бы сохранить непрерывное количество EditTexts. Я добавляю LinearLayouts в методе createNewLinearLayout() ниже и хочу сохранить входные данные из EditTexts из этих LinearLayouts.

Как я могу это сделать?

Myмакет

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/relativeOverall"
        android:orientation="vertical"
        android:layout_centerHorizontal="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        tools:context=".MainActivity">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dp"
            android:id="@+id/relativeLayout">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/addCounter"
                android:text="Add"
                android:layout_marginLeft="0dp" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/saveBtn"
                android:layout_toRightOf="@+id/addCounter"
                android:text="Save Data"
                android:layout_marginLeft="30dp"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btnView"
                android:text="View Data"
                android:layout_marginLeft="30dp"
                android:layout_toRightOf="@+id/saveBtn" />

        </RelativeLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:id="@+id/horizontalLine"
            android:background="#B3B3B3"
            android:layout_below="@+id/relativeLayout"
            android:layout_marginTop="10dp"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/nameEditText"
            android:textAlignment="center"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="0dp"
            android:hint="Name of Patient"
            android:layout_below="@+id/horizontalLine"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1sp"
            android:id="@+id/horizontalLine2"
            android:background="#B3B3B3"
            android:layout_below="@+id/nameEditText"
            android:layout_marginTop="0dp"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/idEditText"
            android:textAlignment="center"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="0dp"
            android:hint="ID of Patient"
            android:layout_below="@+id/horizontalLine2"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1sp"
            android:id="@+id/horizontalLine3"
            android:background="#B3B3B3"
            android:layout_below="@+id/idEditText"
            android:layout_marginTop="0dp"/>

        <EditText
            android:layout_width="245dp"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:id="@+id/actionEditText"
            android:textAlignment="center"
            android:hint="Action To Record"
            android:layout_below="@+id/horizontalLine3"/>

        <EditText
            android:layout_width="245dp"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:id="@+id/durationEditText"
            android:textAlignment="center"
            android:hint="Duration of Action"
            android:layout_below="@+id/actionEditText"/>

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_below="@+id/durationEditText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:orientation="horizontal"
            android:layout_marginTop="0dp">

            <Button
                android:id="@+id/minusBtn"
                android:layout_width="55sp"
                android:layout_height="55sp"
                android:text="-"
                android:textSize="25dp"/>

            <TextView
                android:id="@+id/counterTxt"
                android:layout_width="wrap_content"
                android:layout_height="55dp"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="40dp"
                android:layout_marginRight="40dp"
                android:textSize="45sp"
                android:text="25"/>

            <Button
                android:id="@+id/plusBtn"
                android:layout_width="55sp"
                android:layout_height="55sp"
                android:text="+"
                android:textSize="25sp"/>

        </LinearLayout>

        <Button
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/delete"
            android:layout_below="@+id/linearLayout"
            android:text="Delete"
            android:textAlignment="center"
            android:layout_centerHorizontal="true"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:orientation="vertical"
            android:id="@+id/linearLayoutText"
            android:layout_below="@+id/horizontalLine3">

        </LinearLayout>

    </RelativeLayout>

</ScrollView>

Мой метод

private LinearLayout createNewLinearLayout(){
    final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    lparams.gravity = Gravity.CENTER_HORIZONTAL;

    final ViewGroup.LayoutParams btnParam = plusBtn.getLayoutParams();

    final ViewGroup.LayoutParams textParam = counterTxt.getLayoutParams();

    final LinearLayout.LayoutParams deleteParam = new LinearLayout.LayoutParams(delete.getLayoutParams());
    deleteParam.gravity = Gravity.CENTER_HORIZONTAL;

    final LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(actionEditText.getLayoutParams());
    editParams.gravity = Gravity.CENTER_HORIZONTAL;

    final LinearLayout.LayoutParams durationParams = new LinearLayout.LayoutParams(durationEditText.getLayoutParams());
    durationParams.gravity = Gravity.CENTER_HORIZONTAL;

    LinearLayout.LayoutParams line = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,TypedValue.COMPLEX_UNIT_SP, 1);
    line.setMargins(0,15,0,0);

    final LinearLayout newLinear = new LinearLayout(this);
    newLinear.setLayoutParams(lparams);

    final LinearLayout combined = new LinearLayout(this);
    combined.setLayoutParams(lparams);

    final Button plus = new Button(this);
    plus.setLayoutParams(btnParam);

    final Button minus = new Button(this);
    minus.setLayoutParams(btnParam);

    final TextView count = new TextView(this);
    count.setLayoutParams(textParam);

    final EditText editText = new EditText(this);
    editText.setLayoutParams(editParams);
    editText.setHint("Action To Record");
    editText.setGravity(Gravity.CENTER_HORIZONTAL);

    final View v = new View(this);
    v.setLayoutParams(line);
    v.setBackgroundColor(Color.parseColor("#B3B3B3"));

    final EditText duration = new EditText(this);
    duration.setLayoutParams(durationParams);
    duration.setHint("Duration of Action");
    duration.setGravity(Gravity.CENTER_HORIZONTAL);

    final Button delete = new Button(this);
    delete.setLayoutParams(deleteParam);
    delete.setText("delete");
    delete.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {
            linearLayoutText.removeView(combined);
        }
    });

    plus.setText("+");
    plus.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
    plus.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {
            counting[0]++;
            count.setText(counting[0] + "");
        }
    });

    minus.setText("-");
    minus.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
    minus.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {
            if (counting[0] > 0)
                counting[0]--;
            count.setText(counting[0] + "");
        }
    });

    count.setTextSize(TypedValue.COMPLEX_UNIT_SP, 45);

    newLinear.setOrientation(LinearLayout.HORIZONTAL);
    newLinear.setGravity(Gravity.CENTER_HORIZONTAL);

    combined.setOrientation(LinearLayout.VERTICAL);
    combined.setGravity(Gravity.CENTER_HORIZONTAL);

    newLinear.addView(minus);
    newLinear.addView(count);
    newLinear.addView(plus);

    combined.addView(editText);
    combined.addView(duration);
    combined.addView(newLinear);
    combined.addView(delete);
    combined.addView(v);

    counting[0] = 0;
    count.setText(counting[0] + "");

    return combined;
}

}

1 Ответ

0 голосов
/ 09 октября 2019

Я предлагаю:

        for (int i =0;i<LinearLayout.getChildCount();i++)
        {

            EditText edt= new EditText (this);
            edt = LinearLayout.getChildAt(i);
            YourAarraylist[i]=edt.getText().toString();

        }

Если у вас есть несколько представлений внутри LinearLayout, вы можете повторить следующее:

for (int i =0;i<LinearLayout.getChildCount();i++)
    {
        LinearLayout secondLayout= (LinearLayout) LinearLayout.getChildAt(i);
     //   and then:

        EditText edt= new EditText (this);
        edt = secondLayout.getChildAt(indexofyoutEdittext);//You should know this because it's the order of the views you add in your layout

        YourAarraylist[i]=edt.getText().toString();

    }

Надеюсь, это поможет!

...