1

I want to do the same thing I did here for a String resource:

write(String.format(getString(R.string.night), "Scurcola"));

but for a String Array, defined as follows:

<string-array name="goodMorning" formated="false">
    <item>Good Morning folks!</item>
    <item>Sun\'s out, guns out.</item>
    <item>Careless vampires burn, and you wake up!</item>
    <item>Can\'t find the moon. Oh it\'s probably morning then..</item>
    <item>Darkness flees from %s, wake up!</item>
    <item>Sunlight manifests, it\'s morning %s!</item>
    <item>Stars went to bed, and %s wakes up!</item>
    <item>Be beep. Be beep. %s wake up!</item>
</string-array>

How can I achieve this?

4
  • Every array item per se is actually a string. Therefore, each item can be individually formatted. Commented Jul 10, 2016 at 20:45
  • How, I can't get a reference.. Commented Jul 10, 2016 at 20:54
  • It seems you were successful to make what I suggested. Commented Jul 10, 2016 at 21:10
  • 1
    Uhm yeah, if you want post your answer I'll be happy to accept that one rather than mine @BobMalooga Commented Jul 10, 2016 at 21:26

2 Answers 2

1

I solved this adding this method which edits a TextView:

public void write(int id, int position, String text){
    String[] msg = getResources().getStringArray(id);
    messages.add(String.format(msg[position], text));
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, messages);
    screen.setAdapter(adapter);
}

This way I can simply type:

write(R.array.goodMorning, randInt(0, 7), "Scurcola");

and get as output an item of the String Array, chosen randomly, thus formatted with the "Scurcola" String I passed as a parameter.

Sign up to request clarification or add additional context in comments.

Comments

0

Every array item per se is actually a string.
Therefore, each item can be individually formatted.

I.e.:

write(String.format(getResources().getStringArray(R.array.goodMorning)[0], "Scurcola"));

1 Comment

Hey, I have just opened a new question, maybe you can answer it: stackoverflow.com/questions/38307500/…

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.