How to input data to an ArrayList and show it in a ListView simultaneously? [SOLVED]


For this you need to have an EditText, a submit button and a ListView in your layout.
The user will enter the data in the EditText and when the submit button is pressed the string entered would be stored inside an ArrayList and the corresponding changes will be reflected in the ListView.

So first of all create the following objects in your activity:

EditText input;
Button submit;
ListView show;
public static ArrayList<String> data=new ArrayList<String>();

and then initialize them in the onCreate method:

input=(EditText)findViewById(R.id.edittext1);
submit=(Button)findViewById(R.id.submit_button);
show=(ListView)findViewById(R.id.list1);

then add the following OnClickListener for the submit button. When the button is pressed, the following code will make sure that blank field is not entered into the arrayList, and also that the values aren’t repeated. If you want to allow the repeated values then you can remove the ‘else if’ block.

submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (input.getText().toString()==null || input.getText().toString().trim().equals("")){
                    Toast.makeText(getBaseContext(),"Input Field is Empty", Toast.LENGTH_LONG).show();
                }
                else if (data.contains(input.getText().toString())){
                    Toast.makeText(getBaseContext(),"You've already entered that..", Toast.LENGTH_LONG).show();
                }
                else {
                    data.add(input.getText().toString());
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, data);
                    show.setAdapter(adapter);
                    input.setText("");
                }

            }
        });

Don’t forget to add the EditText, Submit button and the ListView to your layout.

<EditText
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:ems="10"
                android:id="@+id/edittext1"
                android:hint="enter data"/>
<Button
                    android:layout_width="wrap_parent"
                    android:layout_height="wrap_content"
                    android:text="Submit"
                    android:id="@+id/submit_button"/>
<ListView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/list1"
                android:layout_below="@+id/submit_button"/>

Well, that’s it. The above code will let the user enter the input inside the EditText box, and then on clicking the submit button, the input will be stored as a string in a string ArrayList, and the ListView will be refreshed to show the input.

If you have any questions/doubts, drop them in the comments section down below.

PhD researcher at Friedrich-Schiller University Jena, Germany. I'm a physicist specializing in theoretical, computational and experimental condensed matter physics. I like to develop Physics related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.



Leave a Reply

Your email address will not be published. Required fields are marked *