我有一个Spinner s
对象,我想用String数组初始化它,之后我想在AlertDialog.Builder
中设置这个布局
btnImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = getLayoutInflater().inflate(R.layout.sub_activity_menu_item_addcart, null);
Spinner s = (Spinner)view.findViewById(R.id.sub_activity_mene_item_addcart_quantity);
String[] items = new String[]{
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10",
"11", "12", "13", "14", "15",
"16", "17", "18", "19", "20",
};
/* HOW ? */
// s.setValues(items); ????
alertBox.setView(view);
alertBox.setTitle("Add to Cart")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertBox.show();
}
});
步骤
Layout
getLayoutInflater()
Spinner
Layout
值
AlertDialog.Builder
我通过互联网搜索此问题,但没有找到任何合适的解决方案。每个人都建议使用ArrayAdapter填充微调器,但我没有使用ArrayAdapter的操作
答案 0 :(得分:11)
填充Spinner的另一种方法是直接从xml指定String数组。使用Spinner布局文件中的entries
属性
你的微调器
<Spinner
android:layout_width="wrap_content"
android:layout_height="@dimen/spinner_height"
android:id="@+id/yourid"
android:spinnerMode="dropdown"
android:entries="@array/context_view"/>
您的数据存储在strings.xml中,例如
<string-array name="context_view">
<item>ScrollView</item>
<item>WebView</item>
</string-array>
答案 1 :(得分:4)
你可以在里面创建一个带有微调器的alertdialog,就像:
final View update_layout = getLayoutInflater().inflate(
R.layout.update_layout, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Your title");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
Spinner spinner = (Spinner) update_layout.findViewById(R.id.sub_activity_mene_item_addcart_quantity);
String[] items = { "1", "2", "3", "4", "5",
"6", "7", "8", "9", "10",
"11", "12", "13", "14", "15",
"16", "17", "18", "19", "20" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(HomeScreen.this,
android.R.layout.simple_spinner_dropdown_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
builder.setView(update_layout);
AlertDialog alert = builder.create();
alert.show();
答案 2 :(得分:1)
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,items);
spinner.setAdapter(adapter);