How to fill Textview setText from an Arraylist?

Asked

Viewed 1,070 times

1

I get a array with items of interest and I must set them on setText of a particular TextView.

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

My question, do I have to mount a Listview, horizontally, to receive this array and present it on the screen? Or, I can just take the items and put them in the setText property of this Textview?

Follows 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

        }
    });
}
  • Hello Henrique, it is easier to give you a concrete answer if you edit your question and explain what exactly you want to appear in txt_interests. If it can be anything (in any format) the answer can be simply txt_interests.setText(interest.toString()); (that will display something like [Interesse1, Interesse2, Interesse3]).

  • I did, but that’s pretty much what was in the question anyway. I wonder how it can be done so that it is presented as text on the screen, when receiving this array with the items.

  • Oi Henrique, see if the suggestion in the comment above resolves (I think that’s what Victor meant in his reply too). Of course visually a List View with an item for each interest is quite different from a Text View with all interests concatenated into a single String, but what is best depends on the application. Or your question is another?

  • @Anthonyaccioly would be a part of the question of logic, solved by "toString" and which would be better. But I managed to understand here. You can put as answer that mark. Thank you very much!

  • Hi @Henrique, no problem. I think the current answers are already dealing with what I would write, so feel free to accept the one that best fits your problem (assuming it is solved)

2 answers

2


According to your question, I will try to answer simply based on the data and code you have made available.

As far as we can tell, you declared a variable of the type List<String> whose name is interest, that occasionally would be the interests you want to assign to your TextView.

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

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

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

Updating

As our friend pointed out in the commentary, there is an old argument which often appears in Java which is the wrong use 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 a String. Goes below:

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

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

  • 1

    Hi seamusd, good answer. The only implementation detail would be the concatenation inside a loop. While the compiler tries to optimize this kind of thing, it’s worth being explicit and using a StringBuilder.

1

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

  • I didn’t understand your answer. I want to put in the textView setText. I would have to create a list, custom?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.