0

Hey i got a json response which contain strings and arrays as well, fetching strings works fine but when tried to fetch array type data it gvies error in studio.

{
"body": [
    {
       "customer": "erp touch",

      "description": [
        "ythn",
        "bgtr",
        "ythn"
    ]

  }
 ]
}

I am able to fetch customer but could not do it for description, here is the pojo i am using for description

@SerializedName("description")
private List<String> description = null;

public List<String> getDescription() {
    return description;
}

And this is what i am using to get it

 OrderListResponse orderListResponse = response.body().getBody();

 description_tv.setText(orderListResponse.getDescription()); // this line give error cannot resolve setText(java.util.list<java.lang.string>)

NOTE: Please do not get confused with response.body().getBody() because i haven't posted the complete response.

Please tell me how to get this data any help will be appreciable.

THANKS!!

EDIT

Hey all actually i figured out with my mate that how we want to show this data in array, and i am havng problem with that.

I want to fetch this description array from json response and show its different elements in different textviews. Using ,

description_tv1.setText(orderListResponse.getDescription().get(0));
description_tv2.setText(orderListResponse.getDescription().get(1));
description_tv3.setText(orderListResponse.getDescription().get(2));

will resolve the problem but elements in array can vary up to any number so in actual i dont know how many textviews i should use, this is the real problem.

Is there any way that i can create textviews according to my problems ?

Any suggestion or help will appreciated.

THANKS AGAIN !

3
  • of course it will give error you are trying to set List<String> to text view, which is expecting a string Commented Oct 24, 2018 at 12:53
  • 1
    Try this and check description_tv.setText(orderListResponse.getDescription().get(0)); Commented Oct 24, 2018 at 12:54
  • that will give me the first element Commented Oct 24, 2018 at 12:56

2 Answers 2

1

setText does not accept a List as parameter. What you could try is to join the items in your list using .join like this

String result = TextUtils.join(", ", orderListResponse.getDescription());

And then you can call setText(result)


Just a tip: make sure you null check the result and description first!

List<String> description = orderListResponse.getDescription();
if (description == null) { // show error }
Sign up to request clarification or add additional context in comments.

4 Comments

i can null check the result variable because i am description from response.body()
well this is the answer i was looking for but help me the null thing
Hey I edited my question with similar problem i am facing can you please go and check it >
You can not just edit a submission with a new question. Please make a new post on StackOverflow
0

Try using append with TextView

 for (String s : orderListResponse.getDescription()){
                description_tv.append(s);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.