如何将TextView的文本设置为微调器上所选项的内容?

时间:2017-03-04 00:55:20

标签: java android textview android-spinner onitemselectedlistener

我正在尝试将所选项目的值添加到TextView中,并且我希望每次更改所选项目时都要更改它! 我试图将所选项目的位置放入一个字符串,然后将TextView的文本设置为相同的字符串,但没有成功!我真的很喜欢java编码,所有答案都非常受欢迎!

主要活动:

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) {

        }
    }

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)

2 个答案:

答案 0 :(得分:0)

在Toast线上方,放上这一行:

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

但要使其工作,您需要使您的适配器可访问。因此,将适配器设置在" TextView g1"旁边。并将它们放在Activity的顶部,而不是放在监听器内部:

TextView g1;
ArrayAdapter<CharSequence> mAdapter;

修改以回应评论:

将g1和mAdapter放在Activity的顶部:

public class MainActivity {

    TextView mG1;
    ArrayAdapter<CharSequence> mAdapter;
    ...
}

答案 1 :(得分:0)

string.xml

<string-array name="array_name">
<item>Array Item One</item>
<item>Array Item Two</item>
<item>Array Item Three</item>
</string-array>

布局

<Spinner 
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:entries="@array/array_name"
    />

活动类

String[] myarray;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myarray = getResources().getStringArray(R.array.array_name);
}

你的班级

public class MyOnItemSelectedListener  implements AdapterView.OnItemSelectedListener {

    TextView g1;


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



                 String result = myarray[pos]; 
                Toast.makeText(parent.getContext(), "Selected Country : " + result , Toast.LENGTH_SHORT).show();
                g1.setText(result);

        }

        @Override
        public void onNothingSelected(AdapterView parent) {

        }
    }