简体   繁体   English

Android Studio:微调器根本不显示项目。 [XML/Java]

[英]Android Studio: Spinner not showing items at all. [XML / JAVA]

I'm not able to make work a simple android spinner.我无法使工作成为一个简单的 android 微调器。 I followed many examples on inte.net but no one works for me.我遵循了 inte.net 上的许多示例,但没有一个适合我。

Here is what I've done in xml:这是我在 xml 中所做的:

       <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center">
        <Spinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:spinnerMode="dropdown"
            android:theme="@style/ThemeOverlay.AppCompat.Light"
app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>

And here in my java class:在我的 java class 中:

public class PreferencesActivity extends AppCompatActivity {

    Spinner spinner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preferences);

        spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.language_list, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

    }


    @Override
    public void onResume(){
        super.onResume();
        setContentView(R.layout.preferences);
    }

}

And here in my xml res/string:在我的 xml res/string 中:

<resources>

    <string-array name="language_str">
        <item>Language</item>
        <item>Langue</item>
        <item>Lingua</item>
        <item>Idioma</item>
    </string-array>

    <string-array name="language_list">
        <item>English</item>
        <item>Francais</item>
        <item>Italiano</item>
        <item>Espanol</item>
    </string-array>
</resources>

When I debug my application (with real device), the spinner is not shown at all, like as the code never executes.当我调试我的应用程序(使用真实设备)时,微调器根本没有显示,就像代码从未执行过一样。 I read about "change the background color" of the spin but nothing seems to happen.我读到有关旋转的“更改背景颜色”的信息,但似乎什么也没发生。 I tried iserting the key-word "entries" in xml file but nothing happens.我尝试在 xml 文件中插入关键字“条目”,但没有任何反应。

What could be the problem?可能是什么问题呢? Where am I wrong?我哪里错了? Thanks, Marco.谢谢,马可。

Removing the following line from onResume() method will solve your problem从 onResume() 方法中删除以下行将解决您的问题

setContentView(R.layout.preferences);

Replace this line:替换这一行:

ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.language_list, android.R.layout.simple_spinner_item);

With this:有了这个:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.language_list, android.R.layout.simple_spinner_item);

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

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