Search code examples
javaandroidarraysandroid-spinneronitemselectedlistener

onItemSelected for string array


I want to make a spinner that has a list, and when selected an imageView bellow it will change getting its value form its integer-array and also change a TextView that also gets its value from a string-array.

For now i have done 2 of them, and it works (The spinner and changing the image bellow it). The problem is getting the TextView to change. When i run it the app it just crash

My java code :

imgs = getResources().obtainTypedArray(R.array.wakafimage);
image = (ImageView) findViewById(R.id.senaraiwakafimage);
nos = getResources().obtainTypedArray(R.array.nosijil);
tnos = (TextView)findViewById(R.id.sno);

dynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {
            image.setImageResource(imgs.getResourceId(
                    dynamicSpinner.getSelectedItemPosition(), -1));

            tnos.setText(nos.getResourceId(
                    dynamicSpinner.getSelectedItemPosition(), -1));

My string.xml:

<string-array name="wakaf">
    <item>Pembinaan Sekolah Agama di Seluruh Negeri Johor</item>
    <item>Wakaf Bangunan Darul Furqan</item>
    <item>Wakaf Bangunan Kompleks Tahfiz Kluang</item>
    <item>Wakaf Pembelian Mesin Hemodialisis</item>
    <item>Wakaf Bangunan Asrama Pelajar di Jordan</item>
</string-array>

<string-array name="nosijil">
    <item>2015000149861</item>
    <item>2015000149862</item>
    <item>2015000149863</item>
    <item>2015000149864</item>
    <item>2015000149865</item>
</string-array>

<integer-array name="wakafimage">
    <item>@drawable/wsekolah</item>
    <item>@drawable/wfurqan</item>
    <item>@drawable/wtahfiz</item>
    <item>@drawable/wmesin</item>
    <item>@drawable/wasrama</item>

The problem lies here as when i remove it the app works fine.

tnos.setText(nos.getResourceId(
                    dynamicSpinner.getSelectedItemPosition(), -1));

Im new to android, and also its my first question, sorry for bad English and thanks in advance.


Solution

  • Try this

    tnos.setText(nos.getString(dynamicSpinner.getSelectedItemPosition());