Reputation: 674
I have a Spinner s
object and I want to initialize this with array of String, after that i want set this Layout in 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();
}
});
Steps
Layout
by using getLayoutInflater()
Spinner
values inside Layout
AlertDialog.Builder
I have search this problem over the internet but did'nt found any suitable solution. Every body recomend to populate the spinner by using ArrayAdapter but i have no opetion for use ArrayAdapter
Upvotes: 2
Views: 14372
Reputation: 3456
You cand create an alertdialog with a spinner inside, just like :
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();
Upvotes: 4
Reputation: 17899
Another way to populate a Spinner is to directly specified the String array from xml. use the entries
attribute in your Spinner layout file
Your spinner
<Spinner
android:layout_width="wrap_content"
android:layout_height="@dimen/spinner_height"
android:id="@+id/yourid"
android:spinnerMode="dropdown"
android:entries="@array/context_view"/>
Your data stored in strings.xml for example
<string-array name="context_view">
<item>ScrollView</item>
<item>WebView</item>
</string-array>
Upvotes: 16
Reputation: 13731
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,items);
spinner.setAdapter(adapter);
Upvotes: 2