Getting error while using setOnItemSelectedListener in a spinner in a fragment?

1.1k views Asked by At

I want to use a spinner in my android fragment. For the purpose, I wrote the following code:

package com.example.shiza.dailyquranverses;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


/**
 * A simple {@link Fragment} subclass.
 */
public class completeQuran extends Fragment implements AdapterView.OnItemSelectedListener {
    Spinner spinner;


    public completeQuran() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        View view = inflater.inflate(R.layout.fragment_complete_quran, container, false);

        spinner = (Spinner)view.findViewById(R.id.selectChapter);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(),R.array.chapters,android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

        return view;
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        int quran_id;
        String chapter_verse="";
        TextView textView;
        position++;
        String chapter_array_name = "chapter_" + position;

        quran_id = getResources().getIdentifier(chapter_array_name, "array", getActivity().getApplicationContext().getPackageName());
        String[] chapter = getResources().getStringArray(quran_id);

        for ( int item = 0 ; item < chapter.length ; item++ )
        {
//            if ( item > 0 )
//            {
//                chapter_verse += item + ". " ;
//            }
            chapter_verse += chapter[item] + "\n";
        }
        textView = (TextView)view.findViewById(R.id.verse);
        textView.setText(chapter_verse);
        textView.setMovementMethod(new ScrollingMovementMethod());
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent)
    {
        Toast.makeText(getActivity().getApplicationContext(),"Please enter your choice",Toast.LENGTH_LONG).show();
    }
}

I am getting error at

textView.setText(chapter_verse);

in onItemSelected method. I am using its view to get values from xml. The xml corresponding to the fragment looks like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Spinner
        android:id="@+id/selectChapter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/backgroundColor"
        android:orientation="vertical">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:scrollbars="vertical"
            tools:context="com.example.shiza.dailyquranverses.Quran">

            <TextView
                android:id="@+id/verse"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxLines="2000"
                android:scrollbars="vertical"
                android:text="Hello from Quran"
                android:textSize="@dimen/verse_font_chapter" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

Please help me to solve this.

3

There are 3 answers

1
Vodet On

It's because setText eat a charSequence and you give a String you need to use

textView.setText(String.valueOf(chapter_verse))
0
learner On

The issue was with:

textView = (TextView)view.findViewById(R.id.verse);

I need to use getView() method instead of view method.

0
poisondminds On

The onItemSelected callback tells you when a certain item in your Spinner has been selected. The View that exists as a parameter in that callback is the View in the Spinner that was selected. For this reason, I'm guessing that your textView variable will be null. The View that you are trying to access is not enclosed within the View that is being selected, it is enclosed in your layout in general. You should set the reference to that TextView inside of your OnCreateView like so:

public class completeQuran extends Fragment implements AdapterView.OnItemSelectedListener 
{
    Spinner spinner;
    TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
    {

        View view = inflater.inflate(R.layout.fragment_complete_quran, container, false);

        spinner = (Spinner)view.findViewById(R.id.selectChapter);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(),R.array.chapters,android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

        textView = (TextView)view.findViewById(R.id.verse); //notice the view that this is finding your textview within

        return view;
    }

}

Then you can use that reference to textView in your onItemSelected callback. You don't need to redefine the reference every time.