Search code examples
javaandroidandroid-listviewandroid-edittext

How to get a Single line of an EditText, add it to a Listview


I have this 2 lines in the EditText:

%Print "HI"%      
%Print "I love StackOverFlow"%

How to add only this " --)2TEXT's(-- " to a listview?


Solution

  • Try using String.split(). Code example:

    String multiLines = editText.getText().toString();
    String[] streets;
    String delimiter = "\n";
    
    streets = multiLines.split(delimiter);
    

    Now you have an array of streets. Then create an ArrayAdapter like this:

    ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_view_items, R.id.textView, streets);
    

    Then attach the arrayAdapter to your ListView like this:

    ListView simpleList = (ListView) findViewById(R.id.simpleListView);
            simpleList.setAdapter(arrayAdapter);
    

    I would recommend switching to RecyclerView