简体   繁体   English

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

[英]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! 我正在尝试将所选项目的值转换为TextView,并且我希望每次更改所选项目时它都会更改! 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 ! 我尝试将所选项目的位置放入字符串中,然后将TextView的文本设置为与此字符串相同,但没有成功! I'm really really new on java coding, all answer is really welcome ! 我真的是java编码上的新手,真的很欢迎所有答案!

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() : 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) 

Above your line for the Toast, put this line: 在Toast行上方,放置以下行:

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”旁边,并将其放在Activity的顶部而不是侦听器的内部:

TextView g1;
ArrayAdapter<CharSequence> mAdapter;

Edit in response to comment: 编辑以回应评论:

Put the g1 and mAdapter at the top of the Activity: 将g1和mAdapter放在Activity的顶部:

public class MainActivity {

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

string.xml string.xml

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

layout 布局

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

Activity class 活动课

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

your class 你的班

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

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM