我在使用字符串数组中的项填充微调器时遇到问题。当我启动模拟器时,旋转器中没有显示任何内容。显示微调器并且它是空的,但是当我点击它时没有任何反应。
这是我的活动:
package org.example.anders.eazy;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class StartActivity extends Activity {
Spinner workoutSpinner;
ArrayAdapter<CharSequence> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
workoutSpinner = (Spinner)findViewById(R.id.spinnerWorkout);
adapter =ArrayAdapter.createFromResource(this,R.array.workout_name,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
workoutSpinner.setAdapter(adapter);
workoutSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+" selected", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
我的活动布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.example.anders.eazy.StartActivity">
<Button
android:id ="@+id/push_button"
android:layout_width="180dp"
android:layout_height="180dp"
android:text="@string/startWorkout"
android:background="@drawable/button_bg_round"
android:padding="15dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<TextView android:text="@string/workout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/push_button"
android:id="@+id/WorkoutText"/>
<Spinner
android:id="@+id/spinnerWorkout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/WorkoutText"/>
</RelativeLayout>
我在字符串文件中的字符串数组如下所示:
<!-- Spinner options-->
<string-array name="workout_name">
<item>Chest Workout</item>
<item>Leg Workout</item>
<item>Arms Workout</item>
</string-array>
答案 0 :(得分:0)
你能试试吗?
class YourActivity extends AppCompat {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
spinner = (Spinner) findViewById(R.id.spinner_id);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.your_array));
spinner.setAdapter(adapter);
}
}