'how to Save Inputted Values ​from Edittext into Array?

I am making a program to get Values ​​from edittext and store it in an array then I want to display that array on screen and here is my command

 EditText ed1,ed2;
    RadioButton rd1,rd2,rd3,rd4,rd5;
    Button btn1;
    TextView tvkq;



String X=ed2.getText().toString();
int soX=Integer.parseInt(X);
String dayso=ed1.getText().toString().trim();
int soN=Integer.parseInt(dayso);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    anhxa();
    btn1.setOnClickListener(view ->  {
        ed1.setText(" ");
        ArrayList<String> arryList = new ArrayList<String>();
        arryList.add(ed1.getText().toString());
        if (rd1.isChecked()){
            tvkq.setText("Kêt quả:"+arryList);
        }

    });

}

When I run the program, I got an error, please help me



Solution 1:[1]

Try this

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText ed1 = findViewById(R.id.et1);
        Button btn1 = findViewById(R.id.btn1);
        TextView tvkq = findViewById(R.id.tvkq);
        
        btn1.setOnClickListener(view ->  {

            ArrayList<String> arryList = new ArrayList<String>();
            arryList.add(ed1.getText().toString());
                tvkq.setText("Kêt qu?:"+arryList);
        });
    }
}

And the xml file

 <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">



    <TextView
        android:id="@+id/tvkq"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tvkq"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <EditText
        android:id="@+id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        app:layout_constraintBottom_toTopOf="@id/tvkq"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Solution 2:[2]

First problem in your code will be trying to get values from view without initialisation even before XML file is loaded in your activity.

Solution: You suppose to initialize your views under onCreate() and below setContentView(R.layout.activity_main) methods, by providing correct view Id as following;

EditText ed1,ed2;
RadioButton rd1,rd2,rd3,rd4,rd5;
Button btn1;
TextView tvkq;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edt1 = findViewById(R.id.edt1);
    edt2 = findViewById(R.id.edt2);
    btn1 = findViewById(R.id.btn1);
    ......//Initialize other views

}

Second you are clearing text from your edit text before adding it to an array and also declare and initialize ArrayList outside click Listener.

EditText ed1,ed2;
RadioButton rd1,rd2,rd3,rd4,rd5;
Button btn1;
TextView tvkq;
ArrayList<String> arryList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edt1 = findViewById(R.id.edt1);
    edt2 = findViewById(R.id.edt2);
    btn1 = findViewById(R.id.btn1)

    ......//Initialise other views
    arryList = new ArrayList<String>();

    btn1.setOnClickListener(view ->  {
        arryList.add(ed1.getText().toString());
        if (rd1.isChecked()){
           tvkq.setText("Kêt qu?:"+arryList.toString());
        }
        ed1.setText(" ");
    });

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anandh Krishnan
Solution 2