英文:
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:
<?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:
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:
<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.
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<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.language_list, android.R.layout.simple_spinner_item);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论