Setting Multiple Values To Spinner In Android
I can populate more than 1 spinner in Android with multiple values. I want to display more than 1 value in spinner i.e In '0' position (spinner index) I want to set more than one
Solution 1:
You can follow the
http://labs.makemachine.net/2010/03/android-multi-selection-dialogs/ or http://v4all123.blogspot.in/2013/09/spinner-with-multiple-selection-in.html. Let me know if any error?? check this, I think this is what you looking for http://codethis.wordpress.com/2012/06/17/a-spinner-control-for-android-with-multi-select-support/
In MultiSelectionSpinner Class
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.util.AttributeSet;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
publicclassMultiSelectionSpinnerextendsSpinnerimplementsOnMultiChoiceClickListener {
String[] _items = null;
boolean[] mSelection = null;
ArrayAdapter<String> simple_adapter;
publicMultiSelectionSpinner(Context context) {
super(context);
simple_adapter = newArrayAdapter<String>(context,
android.R.layout.simple_spinner_item);
super.setAdapter(simple_adapter);
}
publicMultiSelectionSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
simple_adapter = newArrayAdapter<String>(context,
android.R.layout.simple_spinner_item);
super.setAdapter(simple_adapter);
}
publicvoidonClick(DialogInterface dialog, int which, boolean isChecked) {
if (mSelection != null && which < mSelection.length) {
mSelection[which] = isChecked;
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
} else {
thrownewIllegalArgumentException(
"Argument 'which' is out of bounds.");
}
}
@OverridepublicbooleanperformClick() {
AlertDialog.Builderbuilder=newAlertDialog.Builder(getContext());
builder.setMultiChoiceItems(_items, mSelection, this);
builder.show();
returntrue;
}
@OverridepublicvoidsetAdapter(SpinnerAdapter adapter) {
thrownewRuntimeException(
"setAdapter is not supported by MultiSelectSpinner.");
}
publicvoidsetItems(String[] items) {
_items = items;
mSelection = newboolean[_items.length];
simple_adapter.clear();
simple_adapter.add(_items[0]);
Arrays.fill(mSelection, false);
}
publicvoidsetItems(List<String> items) {
_items = items.toArray(newString[items.size()]);
mSelection = newboolean[_items.length];
simple_adapter.clear();
simple_adapter.add(_items[0]);
Arrays.fill(mSelection, false);
}
publicvoidsetSelection(String[] selection) {
for (String cell : selection) {
for (intj=0; j < _items.length; ++j) {
if (_items[j].equals(cell)) {
mSelection[j] = true;
}
}
}
}
publicvoidsetSelection(List<String> selection) {
for (inti=0; i < mSelection.length; i++) {
mSelection[i] = false;
}
for (String sel : selection) {
for (intj=0; j < _items.length; ++j) {
if (_items[j].equals(sel)) {
mSelection[j] = true;
}
}
}
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
}
publicvoidsetSelection(int index) {
for (inti=0; i < mSelection.length; i++) {
mSelection[i] = false;
}
if (index >= 0 && index < mSelection.length) {
mSelection[index] = true;
} else {
thrownewIllegalArgumentException("Index " + index
+ " is out of bounds.");
}
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
}
publicvoidsetSelection(int[] selectedIndicies) {
for (inti=0; i < mSelection.length; i++) {
mSelection[i] = false;
}
for (int index : selectedIndicies) {
if (index >= 0 && index < mSelection.length) {
mSelection[index] = true;
} else {
thrownewIllegalArgumentException("Index " + index
+ " is out of bounds.");
}
}
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
}
public List<String> getSelectedStrings() {
List<String> selection = newLinkedList<String>();
for (inti=0; i < _items.length; ++i) {
if (mSelection[i]) {
selection.add(_items[i]);
}
}
return selection;
}
public List<Integer> getSelectedIndicies() {
List<Integer> selection = newLinkedList<Integer>();
for (inti=0; i < _items.length; ++i) {
if (mSelection[i]) {
selection.add(i);
}
}
return selection;
}
private String buildSelectedItemString() {
StringBuildersb=newStringBuilder();
booleanfoundOne=false;
for (inti=0; i < _items.length; ++i) {
if (mSelection[i]) {
if (foundOne) {
sb.append(", ");
}
foundOne = true;
sb.append(_items[i]);
}
}
return sb.toString();
}
public String getSelectedItemsAsString() {
StringBuildersb=newStringBuilder();
booleanfoundOne=false;
for (inti=0; i < _items.length; ++i) {
if (mSelection[i]) {
if (foundOne) {
sb.append(", ");
}
foundOne = true;
sb.append(_items[i]);
}
}
return sb.toString();
}
}
Solution 2:
Blow is the solution :-
package com.kazi.createActivity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.util.AttributeSet;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.kazi.R;
import java.util.List;
publicclassMultiSpinnerextendsSpinnerimplementsOnMultiChoiceClickListener, DialogInterface.OnCancelListener {
private List<String> items;
privateboolean[] selected;
private String defaultText;
private MultiSpinnerListener listener;
publicMultiSpinner(Context context) {
super(context);
}
publicMultiSpinner(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
}
publicMultiSpinner(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
}
@OverridepublicvoidonClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked)
selected[which] = true;
else
selected[which] = false;
}
@OverridepublicvoidonCancel(DialogInterface dialog) {
// refresh text on spinnerStringBufferspinnerBuffer=newStringBuffer();
booleansomeUnselected=false;
for (inti=0; i < items.size(); i++) {
if (selected[i] == true) {
spinnerBuffer.append(items.get(i));
spinnerBuffer.append(", ");
} else {
someUnselected = true;
}
}
String spinnerText;
if (someUnselected) {
spinnerText = spinnerBuffer.toString();
if (spinnerText.length() > 2)
spinnerText = spinnerText.substring(0, spinnerText.length() - 2);
} else {
spinnerText = defaultText;
}
ArrayAdapter<String> adapter = newArrayAdapter<String>(getContext(),
R.layout.spinner_list_item,
newString[] { spinnerText });
setAdapter(adapter);
listener.onItemsSelected(selected);
}
@OverridepublicbooleanperformClick() {
AlertDialog.Builderbuilder=newAlertDialog.Builder(getContext());
builder.setMultiChoiceItems(
items.toArray(newCharSequence[items.size()]), selected, this);
builder.setPositiveButton(android.R.string.ok,
newDialogInterface.OnClickListener() {
@OverridepublicvoidonClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setOnCancelListener(this);
builder.show();
returntrue;
}
publicvoidsetItems(List<String> items, String[] allText,
MultiSpinnerListener listener) {
this.items = items;
this.listener = listener;
// all selected by default
selected = newboolean[items.size()];
for (inti=0; i < selected.length; i++)
selected[i] = false;
// all text on the spinner
ArrayAdapter<String> adapter = newArrayAdapter<String>(getContext(),
R.layout.spinner_list_item, allText );
setAdapter(adapter);
}
publicinterfaceMultiSpinnerListener {
publicvoidonItemsSelected(boolean[] selected);
}
}
I created this xml layout:-
<TextView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/spiner_item"android:layout_width="match_parent"android:layout_height="@dimen/spiner_height"android:background="@drawable/rounded_edittext"android:gravity="center_vertical"android:paddingLeft="5dp"android:text="Medium Text"android:textAppearance="?android:attr/textAppearanceMedium"android:textColor="@android:color/black" />
Post a Comment for "Setting Multiple Values To Spinner In Android"