Android Studio:Spinner完全不显示项目。[XML / JAVA]

huangapple go评论124阅读模式
英文:

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

问题

我无法使一个简单的安卓下拉列表(Spinner)工作。我在互联网上查看了很多示例,但没有一个适用于我。

以下是我在 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>

在我的 Java 类中:

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);
    }
}

在我的 XML 资源文件中:

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

当我调试我的应用程序(使用真实设备)时,下拉列表(Spinner)根本不显示,就好像代码从未执行过。我阅读了关于“更改下拉列表背景颜色”的内容,但似乎没有任何反应。我尝试在 XML 文件中插入关键词“entries”,但没有任何效果。

问题可能出在哪里?我做错了什么?
谢谢,
Marco。

英文:

I'm not able to make work a simple android spinner. I followed many examples on internet but no one works for me.

Here is what I've done in xml:

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

And here in my 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:

&lt;resources&gt;

    &lt;string-array name=&quot;language_str&quot;&gt;
        &lt;item&gt;Language&lt;/item&gt;
        &lt;item&gt;Langue&lt;/item&gt;
        &lt;item&gt;Lingua&lt;/item&gt;
        &lt;item&gt;Idioma&lt;/item&gt;
    &lt;/string-array&gt;

    &lt;string-array name=&quot;language_list&quot;&gt;
        &lt;item&gt;English&lt;/item&gt;
        &lt;item&gt;Francais&lt;/item&gt;
        &lt;item&gt;Italiano&lt;/item&gt;
        &lt;item&gt;Espanol&lt;/item&gt;
    &lt;/string-array&gt;
&lt;/resources&gt;

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.

What could be the problem? Where am I wrong?
Thanks,
Marco.

答案1

得分: 1

从onResume()方法中删除以下行将解决您的问题:

setContentView(R.layout.preferences);
英文:

Removing the following line from onResume() method will solve your problem

setContentView(R.layout.preferences);

答案2

得分: 0

使用以下替代:

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

Replace this line:

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

With this:

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

huangapple
  • 本文由 发表于 2020年8月4日 21:05:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63247558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定