0

I've got simple setter and getter of string array. I want to use setter to put some retrieved json info + same text to array. When I use this code:

met.setPlacepic(new String[]{"http://dfsdfsdfsf/" + json.getString("source")});

it looks like setter put only one string to array, despite there is many more data.

Declaration is simple:

public String[] placepic

and the setter is also simple:

public void setPlacepic(String[] placepic) {
        this.placepic = placepic;
}

Does anybody know the reason of this?

5
  • It did put one string because you only gave one string. What did you expect? Commented Aug 9, 2015 at 12:36
  • This is not an Android question but more about a question about Java's arrays. Have a look at and you'll find out how they work: docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html Commented Aug 9, 2015 at 12:36
  • this method is inside of loop which retrevie more than one string Commented Aug 9, 2015 at 12:38
  • So what? what you do in each iteration is provide a new array to your setPlacepic(). Do you want to add to it instead? Commented Aug 9, 2015 at 12:42
  • 1
    i want do store every data in one array, please help i'm stuck Commented Aug 9, 2015 at 12:44

1 Answer 1

1

If the number of strings is fixed (you know exactly how many element you would have in the array), then you could use String Arrays:

String[] placepic = new String[20]; //20 strings
//Then, in your loop:
placepic[i] = yourData;

If you do NOT know how many strings in your data, You should use List:

List<String> placepicList= new ArrayList<String>();
//Then, in your loop:
placepicList.add(yourData);
//Then after the loop, you get the array
String[] placepic = placepicList.toArray(new String[placepicList.size()]);
Sign up to request clarification or add additional context in comments.

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.