javaandroidtextviewandroid-spinneronitemselectedlistener

How to set the text of a TextView to the content of a selected item on a spinner?


I'm trying to get the value of the selected item into a TextView, and i want it to change each time the selected item change! I've try to get the position of the selected item into a string then set the text of the TextView equal to this same string but no sucess ! I'm really really new on java coding, all answer is really welcome !

Main Activity:

package com.barcala.android.smiteultimatecountdown;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
import static com.barcala.android.smiteultimatecountdown.R.array.godsNames;
import static com.barcala.android.smiteultimatecountdown.R.id.god_List;
import static com.barcala.android.smiteultimatecountdown.R.id.selected_God;
import static com.barcala.android.smiteultimatecountdown.R.string.selected_god;


public class ultimate extends AppCompatActivity {
        Spinner gl;
        String god1;
        TextView selectedGod;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ultimate);
        selectedGod = (TextView) findViewById(R.id.selected_God);
        selectedGod.setText(selected_god);
        addListenerOnSpinnerItemSelection();

    }


    public void addListenerOnSpinnerItemSelection() {
        gl = (Spinner) findViewById(R.id.god_List);
        gl.setOnItemSelectedListener(new MyOnItemSelectedListener());


    }

    public void get_name(View view) {
        Spinner god_List = (Spinner) findViewById(R.id.god_List);
        god1 = god_List.getSelectedItem().toString();
        TextView selectedGod = (TextView) findViewById(R.id.selected_God);
        selectedGod.setText(god1);
    }
}

MyOnItemSelectedListener() :

package com.barcala.android.smiteultimatecountdown;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.TextView;

import org.w3c.dom.Text;

import static android.R.attr.y;
import static com.barcala.android.smiteultimatecountdown.R.id.god_List;
import static com.barcala.android.smiteultimatecountdown.R.id.selected_God;
import static com.barcala.android.smiteultimatecountdown.R.string.selected_god;


public class MyOnItemSelectedListener extends ultimate implements AdapterView.OnItemSelectedListener {


        @Override
        public void onItemSelected(AdapterView parent, View view, int pos, long id) {

            String result = parent.getItemAtPosition(pos).toString();

            selectedGod.setText(result);
            Toast.makeText(parent.getContext(), "Selected God : " + result , Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView parent) {

        }
    }

The logcat:

03-05 15:12:09.966 19007-19007/com.barcala.android.smiteultimatecountdown E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                            Process: com.barcala.android.smiteultimatecountdown, PID: 19007
                                                                                            java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                                at com.barcala.android.smiteultimatecountdown.MyOnItemSelectedListener.onItemSelected(MyOnItemSelectedListener.java:26)
                                                                                                at android.widget.AdapterView.fireOnSelected(AdapterView.java:1124)
                                                                                                at android.widget.AdapterView.access$200(AdapterView.java:54)
                                                                                                at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:1089)
                                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                at android.os.Looper.loop(Looper.java:145)
                                                                                                at android.app.ActivityThread.main(ActivityThread.java:5951)
                                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)


Solution

  • Above your line for the Toast, put this line:

    g1.setText(mAdapter.getItem(adapterView.getSelectedItemPosition()));
    

    But for it to work you need to make your adaptor accessible. So set the adapter up next to the "TextView g1" and put them at the top of the Activity rather than inside the listener:

    TextView g1;
    ArrayAdapter<CharSequence> mAdapter;
    

    Edit in response to comment:

    Put the g1 and mAdapter at the top of the Activity:

    public class MainActivity {
    
        TextView mG1;
        ArrayAdapter<CharSequence> mAdapter;
        ...
    }