简体   繁体   中英

How to dynamically keep on adding items from an EditText field?

I am trying to add the text written in a textfield everytime and add it into an ArrayList. For eg- if the text "abcdef" is written in the textfield, then it should get added into the list. Again if the text "ghijkl" is written in the textfield, than that should get added.

My code for adding the text into the listview:

final ArrayList<String> mylist = new ArrayList<String>(5);
String s3=text1.getText().toString();
            if(s3!=null)
                g++;
            else g--;
            if(g>0) {
                for(int i=0;i<mylist.size();i++) {
                    mylist.add(s3);
                    Toast.makeText(getApplicationContext(), mylist.get(i), Toast.LENGTH_LONG).show();
                }
            }

Is this the right way to do this? Also I am trying to get the list item for testing purpose, but I don't get any toast.

I assume you like to add the Content of your EditText field into the list after pressing a button, so you could do it like this:

    // Member to store your Texts in list
    private ArrayList<String> _list = new ArrayList<>()

    // Method to call to add an entry to your list
    // Either by pressing a button, removing focus of your edit text or something else
    private void addTextToList()
    {
        String text = text1.getText().toString();
        if (text != null)
        {
            _list.add(text);
        }
    }

    private void showToast()
    {
        for(String text: _list)
        {
            Toast.makeText(this, mylist.get(i), Toast.LENGTH_LONG).show();
        }
    }

The toast is not shown because you need to pass your current activity. Assuming your Code is in your activity, I passed this in the example above.

you have to make a button to handle this action every click i make editTex ,textView and Button this is the action of button it every click take the value of editText and stor it in ArrayList and increse String by this value

public void getText(View v){

    if(ed.getText().toString().length()!=0){
        s+="\n"+ed.getText().toString();
        arr.add(ed.getText().toString());
    }
    tv.setText(s);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM