0

I want to get the name of my string selected on a spinner.

This is my code to init the spinner;

exam_reason_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    exam_reason_adapter.add(getResources().getString(R.string.programmed));
    exam_reason_adapter.add(getResources().getString(R.string.valve_installation));
    exam_reason_adapter.add(getResources().getString(R.string.improvised));
    reason_status.setAdapter(exam_reason_adapter);

This is how I implement it

reason_status.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            reason_checked = reason_status.getSelectedItem().toString();
            int reasonID = getResources().getIdentifier(reason_checked, "string", null);
            reason = getResources().getResourceName(reasonID).replaceAll("_", "-"); //I need the string with "-" to add it in my jsonRequest.
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

But when I launch my app , i just get this exception: android.content.res.Resources$NotFoundException: Unable to find resource ID #0x0 All the strings values are defined.

EDIT : The Logcat

26413-26413/esisar.test.com.essai E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x0
        at android.content.res.Resources.getResourceName(Resources.java:1648)
        at esisar.pi13.com.essai.HomeActivity$6.onItemSelected(HomeActivity.java:176)
        at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
        at android.widget.AdapterView.access$200(AdapterView.java:49)
        at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

The line 176 is this code:

reason = getResources().getResourceName(reasonID).replaceAll("_", "-");
Ideor
  • 5
  • 5
  • Most likely `getIdentifier()` doesn't find your resource by name, can you add some logging? – ci_ Jun 03 '15 at 11:52
  • You are getting selected item name here "reason_checked = reason_status.getSelectedItem().toString(); " then why you are writing "int reasonID = getResources().getIdentifier(reason_checked, "string", null); reason = getResources().getResourceName(reasonID).replaceAll("_", "-");" ?? – Anand Singh Jun 03 '15 at 11:53
  • reason_checked contains the value of R.string. **XXXX** , and me I want to get the **XXXX**. – Ideor Jun 03 '15 at 11:59
  • 1
    Try this: int reasonID = getResources().getIdentifier(reason_checked, "string", getPackageName()); – Anand Singh Jun 03 '15 at 12:05
  • @Ideor I don't think what you're trying to do is possible, see here http://stackoverflow.com/questions/3904531/get-resource-id-from-value – ci_ Jun 03 '15 at 13:02
  • I just want the value on R.string.value , nothing more, do you think it's impossible ? – Ideor Jun 03 '15 at 13:25
  • @Ideor Take a look at my answer, it might help you. – Erick Filho Jul 17 '15 at 14:18

1 Answers1

0

int reasonID = getResources().getIdentifier(reason_checked, "string", null);

The third argument can't be null. If it's null it doesn't know where to search to get the id. This should solve your problem:
int reasonID = getResources().getIdentifier(reason_checked, "string", getPackageName());

Erick Filho
  • 1,962
  • 3
  • 18
  • 31