简体   繁体   中英

How to set a text line by line to multi line edit text

I have a list of names and i need to add those names to multi line edit text box by line by line. But here it is adding only last added name to edit text box.How can i resolve this can any give a suggestion.

Here my code is:

 for(int i =0; i<Products.length; i++){

                    etItem.setText(Products[i]);
                }
String str = "";
for(int i =0; i<Products.length; i++){
  str  += Products[i]+"\n";
}
etItem.setText(str);

This could be as easy as:

etItem.setText(TextUtils.join("\n", Products));

You'll probably want to guard this with a null check too.

Your own code example sets the text for every product item, overwriting the previous value. Hence, only the last product item will ever display.

Alternatively, an approach that more resembles your original code would be to leverage the TextView#append(...) method. That'll be slightly more code than the one-liner above, as you'll have to ensure there is no line break before the first item (or after the last).

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