1

I want to use Android's plurals resource.

Most questions out there mentioned that "one" is working but the other quantities are not;
But I am encountering the opposite...

Strings.xml

<plurals name="imagepicker_multiselect_not_enough">
    <item quantity="one">You have to select %d more image</item>
    <item quantity="other">You have to select %d more images</item>
</plurals>
<plurals name="imagepicker_multiselect_enough">
    <item quantity="one">You have selected %d image</item>
    <item quantity="other">You have selected %d images</item>
</plurals>

Note that I also tried 1 image instead of %d image.

SomeFragment.java

        if (newCount < minimumMultiSelectCount) {
            tvMultiSelectMessage.setText(getContext().getResources().getQuantityString(
                    R.plurals.imagepicker_multiselect_not_enough,
                    minimumMultiSelectCount - newCount,
                    minimumMultiSelectCount - newCount));
        } else {
            tvMultiSelectMessage.setText(getContext().getResources().getQuantityString(
                    R.plurals.imagepicker_multiselect_enough, newCount, newCount));
        }

I am always getting "You have selected 1 images" or "You have to select 1 more images"...

I made sure that this is the only place tvMultiSelectMessage has been called.

I also tried clean and rebuild, uninstall and reinstall, no luck...

Any idea?!

UPDATE

Just now I tried to change other to few, and I got this Exception:

Resources$NotFoundException: Plural resource ID #0x7f0c0001 quantity=1 item=other

quantity=1 item=other
quantity=1 item=other.....
quantity=1 item=other!!!!

Should I already treat it as bug?

Sira Lam
  • 5,179
  • 3
  • 34
  • 68
  • 1
    actually, your code is ok and you should get what you desire, I'm not sure why you are getting that, what I feel like there would be a very simple mistake you did that causes this behavior. kindly revise the code carefully and make sure you don't do any modifications after set, and about the values of your `minimumMultiSelectCount` and `newCount` – Muhammed Refaat May 09 '18 at 05:54
  • @MuhammedRefaat There is one way I can prove there is no stupid mistake... In my whole project there is only this string resource which has the word "You have to select"; it is correctly `1` but it still uses `images`. In other words, assume I have some stupid mistakes, how can that mistake generate the result `You have to select 1 more images`? – Sira Lam May 09 '18 at 06:19
  • actually I even tried your code in a simple application and everything is working perfectly, I think you should debug at that line to see what is going one, something else, try to add a pretty new string fields and replace them in setting the TextView to test if you are getting the same result. – Muhammed Refaat May 09 '18 at 07:18
  • Muhammed is right, the code works on my end too. you can try debugging into the getQuantityString() method? – Angel Koh May 09 '18 at 07:35
  • 3
    With the above code, I get **1 images** when I set my test device language as Chinese and **1 image** when I set my test device language as English. Hope that helps! – i_A_mok May 10 '18 at 06:35
  • 1
    @I_A_Mok Guessed it! Thank you for your answer... This behavior does not make sense at all.... Mind posting an answer and let me accept it? – Sira Lam May 10 '18 at 06:37
  • 1
    In my case it is because of app localization. if in your application use English as Locale, those code should works. otherwise possibly does not work. – hakim Feb 25 '21 at 13:24

0 Answers0