-1

In my application I want use ChipView, from this Library : https://github.com/adroitandroid/ChipCloud
In this library for set lists , I should use string[].
In my application I get lists of Tag with this code :

response.body().getData().getTags()

And Tags model is :

@SerializedName("tags")
@Expose
private List<NewsDetailTag> tags = null;
...
public List<NewsDetailTag> getTags() {
    return tags;
}

In above library I should add list with this codes :

chipCloud.addChips(someStringArray);

How can I convert List to string[] in android?

Please help me guys.

  • 2
    Possible duplicate of [Convert list to array in Java](https://stackoverflow.com/questions/9572795/convert-list-to-array-in-java) – Mateus Gondim Jun 15 '17 at 14:33

4 Answers4

2

There is no need for a "conversion" at all! No need to waste memory :)

Take a look at the code of the library and see, what ChipCloud.addChips() does:

public void addChips(String[] labels) {
    for (String label : labels) {
        addChip(label);
    }
}

Its just going through the elements of the array and adding each string individually with the addChip() method.

In your code, you can do this the same way with a list:

List<NewsDetailTag> tags;
String              tagString;
ChipCloud           chipCloud;

// Get the tags, initialize the chipCloud, etc ...

for (NewsDetailTag tag : tags) {
    tagString = tag.getTheStringFromNewsDetailTag();
    chipCloud.addChip(tagString);
}

You could even write your own class that extends ChipCloud and add a method that accepts a List parameter.

The only thing thats left to do is to get a String from your NewsDetailTags. But it looks like they are serializable anyways.

lupz
  • 3,067
  • 2
  • 25
  • 40
  • Thanks my friend, when use your code show me this error : `void com.adroitandroid.chipcloud.ChipCloud.addChip(java.lang.String)' on a null object reference` for this line : `chipCloud.addChip(tagString);` . but when use this line : `Log.e("Tag", tag.getName())` show me all tags from server! Why not set into `chipCloud.addChip(tagString); ` ? –  Jun 15 '17 at 15:01
  • can you help me my friend? –  Jun 15 '17 at 15:06
  • Your `ChipCloud` reference is `null`. You can't invoke methods on a null reference. When you defined the chipcloud in an xml layout you can use `ChipCloud chipCloud = (ChipCloud) this.findViewById(R.id.the_id_of_your_chipcloud);` to get the instance from within an activity. – lupz Jun 15 '17 at 15:55
1

try this:

String[] newList = yourList.toArray(new String[]);

hope this works

Meikiem
  • 1,546
  • 2
  • 9
  • 18
1
List<NewsLineTag> tags = response.body().getData().getTags();
List<String> tagStrings = new ArrayList<String>();
//add some stuff
for (NewsLineTag tag : tags) {
    tagStrings.add(tag.getSomeTextValueINeed());
}
chipCloud.addChips(tagStrings.toArray(new String[0]));

getSomeTextValueINeed() should be replaced with some method which will provide you with the String you want to show.

Duplicate of Converting 'ArrayList<String> to 'String[]' in Java

vbkn
  • 108
  • 8
  • please edit your code with my above codes. please. I am amateur –  Jun 15 '17 at 14:36
  • I use this code but show me error : `List list = new ArrayList(); list.addAll(response.body().getData().getTags()); String[] stringArray = list.toArray(new String[0]);` –  Jun 15 '17 at 14:41
  • Can you send me true code with my above codes? please –  Jun 15 '17 at 14:47
  • I've edited the code so you can use it for your case. – vbkn Jun 15 '17 at 14:53
  • Thanks my friend, but show me this error : `NewsDetailTag cannot be stored in destination array of type java.lang.String[]` –  Jun 15 '17 at 14:57
  • can you help me? –  Jun 15 '17 at 15:06
  • Check the edited answer above, please. You can't store NewsDetailTag as String since it's not a String. You need to extract String value you need/want from NewsDetailTag. – vbkn Jun 15 '17 at 15:18
  • In this line you use `chipCloud.addChips(list.toArray(new String[0]));` . what is `list`? where you initialize `list` ? –  Jun 15 '17 at 15:23
  • Sorry, my bad. It should be `tagStrings`, not `list`. Also, please take a look at @lupz answer since it's much cleaner way of using ChipCloud API. I wasn't aware of it (haven't used it at all) so just wrote it down as you already wanted to. – vbkn Jun 15 '17 at 15:40
0

Java's List has a pretty convenient toArray() method you can use to convert a List to an Array of the same type.

However, since you have a List<NewsDetailTag> you will have to build the new array yourself.

It will look something like this:

String[] strings = new String[](list.size())
for(int i = 0; i < list.size(); i++) {
    array[i] = list.get(i).getStringField();
}

Where getStringField() is whatever property on NewsDetailTag contains the String you want.

Bryan Herbst
  • 62,910
  • 10
  • 119
  • 113
  • Thank your my dear friend, can you edit your codes with my above codes? please. I am amateur and I need this. please –  Jun 15 '17 at 14:35
  • I've given you as much as I can with the code you have posted. If you want to represent your `NewsDetailTag `s as Strings, you need to decide what that String representation looks like, and that's what you put in to the array. – Bryan Herbst Jun 15 '17 at 14:36