Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getQuantityString not replacing the format with the value

Tags:

android

I want to uses the plurals resource to produce a quoted number like "9".

In my plurals.xml:

<plurals name="posts">   <item quantity="other">\"%dd\"<\item> </plurals> 

The code:

String text = res.getQuantityString(R.plurals.posts, meUser.postCount);  

When the postCount is 9, why does text turn out to be "%dd" and not "9"?

like image 759
Rose Perrone Avatar asked May 22 '13 21:05

Rose Perrone


People also ask

How do you handle plurals in Android?

Multiple quantities in one string If your display text has multiple quantities, e. g. %d match(es) found in %d file(s). , split it into three separate resources: %d match(es) ( plurals item) %d file(s) ( plurals item)

What is plural string Android?

This enables you to define different strings based on the number of items you refer to. For example, in English you would refer to "one Android" but "seven Androids". By creating a plurals resource, you can specify an alternative string for any of zero, one, multiple, few, many, or other quantities.

What is string xml file?

String Array. XML resource that provides an array of strings. Quantity Strings (Plurals) XML resource that carries different strings for pluralization. All strings are capable of applying some styling markup and formatting arguments.


1 Answers

From the Android docs:

When using the getQuantityString() method, you need to pass the count twice if your string includes string formatting with a number. For example, for the string %d songs found, the first count parameter selects the appropriate plural string and the second count parameter is inserted into the %d placeholder. If your plural strings do not include string formatting, you don't need to pass the third parameter to getQuantityString.

ie res.getQuantityString(R.plurals.numberOfSongsAvailable, count, count);

like image 59
Rose Perrone Avatar answered Oct 18 '22 13:10

Rose Perrone