How to fill TextText setText from an ArrayList?

1

I get an array with interest items and should set them to setText of a given TextView .

I want all items in this array to appear on the screen, horizontally. In my layout xml, I just put 1 textView to get all the items that come in this array.

My question, do I have to mount a ListView, horizontally, to receive this array and display it on the screen? Or, can I just grab the items and put them in the setText property of this TextView?

Follow the code:

@BindView(R.id.txtInterests)
TextView txt_interests;

CommunityPresenter presenter;
UserCommunity selectedUser;
List<String> interest = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setEnterTransition(new Fade());
    }
    setContentView(R.layout.activity_ride_request);

    ButterKnife.bind(this);
    presenter = new CommunityPresenter(this);

    first.setText(presenter.getDay(0));
    second.setText(presenter.getDay(1));
    third.setText(presenter.getDay(2));

    user = new SessionManager();

    selectedUser = CommunityService.i(getContext()).selectedUser;
    reloadView();

    send_offer_request.setOnClickListener(presenter.sendRequestOnClickListener(0));
    send_ask_request.setOnClickListener(presenter.sendRequestOnClickListener(1));

    populateInterests();


}

private void populateInterests() {
    RequestManager.UsersInterests(selectedUser.id, new FutureCallback<String>() {
        @Override
        public void onCompleted(Exception e, String result) {

            //array com os interesses
            interest = new Gson().fromJson(new JsonParser().parse(result).getAsJsonObject().get("data").toString(), new TypeToken<ArrayList<String>>() {
            }.getType());

            txt_interests.setText(); //preencher o TextView com os interesses

        }
    });
}
    
asked by anonymous 14.12.2016 / 17:08

2 answers

2

According to your questioning, I will try to answer in a simple way based on the data and code that you have made available.

So we noticed, you declared a variable of type List<String> whose name is interest , which would occasionally be the interests you want to assign to your TextView .

In order for you to capture each item, you must scroll through your entire list. There are several ways you can go through and capture elements from a list and one of them is to use foreach . Here is a very basic example:

String interests = "";
for(String str: interest){
    interests+="\n"+str;
}
txt_interests.setText(interests);

As preferably yours, wanting the items to appear horizontally, I enter a \n to make a line break in which the next "interest" will appear one below the other.

Update

As our friend pointed out in the commentary, there is an old discussion that often appears in Java which is the misuse of Strings concatenation, which can lead to a loss of performance and trashing from memory .

The use of the + operator seems innocent, but the generated code produces some surprises. Using a StringBuilder for concatenation can actually produce a code that is significantly faster than using String . Follow below:

StringBuilder interests = new StringBuilder();
for(String str: interest){
    interests.append("\n").append(str);
}
txt_interests.setText(interests); 

To learn more details, you can read this article that talks about the differences between String , StringBuilder and StringBuffer in Java.

    
14.12.2016 / 21:48
1

If you want to add everything to a TextView only, just concatenate the information in your list and add it to setText .

    
14.12.2016 / 17:24