Sheriff Said Elahl Sheriff Said Elahl - 6 months ago 140
Android Question

Android Spinner With Multiple Selection

I am new to android and I was trying all the day to apply spinner with multiple selections from the below code (accepted answer on this question Android Spinner with multiple choice

the problem is I can't pass the listener from the main activity to the spinner Java file by the below line:

multiSpinner.setItems(MultiSpinnerList, modifiedcoresitestring, this);


the argument (this) isn't acceptable, I also tried MainActivity.this with no success. what is the problem here?

the Set method in the spinner code should receive (listener) but I don't know what is it and how to send it.

the spinner Java Code:

public class MultiSpinner extends Spinner implements
DialogInterface.OnMultiChoiceClickListener, DialogInterface.OnCancelListener {

private List<String> items;
private boolean[] selected;
private String defaultText;
private MultiSpinnerListener listener;

public MultiSpinner(Context context) {
super(context);
}

public MultiSpinner(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
}

public MultiSpinner(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
}

@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked)
selected[which] = true;
else
selected[which] = false;
}

@Override
public void onCancel(DialogInterface dialog) {
// refresh text on spinner
StringBuffer spinnerBuffer = new StringBuffer();
boolean someUnselected = false;
for (int i = 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 = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_spinner_item,
new String[] { spinnerText });
setAdapter(adapter);
listener.onItemsSelected(selected);
}

@Override
public boolean performClick() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMultiChoiceItems(
items.toArray(new CharSequence[items.size()]), selected, this);
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setOnCancelListener(this);
builder.show();
return true;
}

public void setItems(List<String> items, String allText,
MultiSpinnerListener listener) {
this.items = items;
this.defaultText = allText;
this.listener = listener;

// all selected by default
selected = new boolean[items.size()];
for (int i = 0; i < selected.length; i++)
selected[i] = true;

// all text on the spinner
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_spinner_item, new String[] { allText });
setAdapter(adapter);
}

public interface MultiSpinnerListener {
public void onItemsSelected(boolean[] selected);
}
}


Activity Code:

public class DCOReader extends AppCompatActivity {

private Toolbar Toolbar;
private TextView StateText;
private TextView StateTextDesc;
private Button ReaderGo;
private List<String> DCOID = new ArrayList<>();
private List<String> ActivityDate = new ArrayList<>();
private List<String> CoreSite = new ArrayList<>();
private List<String> Snags = new ArrayList<>();
private List<String> SnagDesc = new ArrayList<>();
private List<String> Layout = new ArrayList<>();
private List<String> LayoutDesc = new ArrayList<>();
private List<String> Mapping = new ArrayList<>();
private List<String> MappingDesc = new ArrayList<>();
private List<String> MailSubject = new ArrayList<>();
private List<String> MDCOID = new ArrayList<>();
private List<String> MActivityDate = new ArrayList<>();
private List<String> MCoreSite = new ArrayList<>();
private List<String> MSnags = new ArrayList<>();
private List<String> MSnagDesc = new ArrayList<>();
private List<String> MLayout = new ArrayList<>();
private List<String> MLayoutDesc = new ArrayList<>();
private List<String> MMapping = new ArrayList<>();
private List<String> MMappingDesc = new ArrayList<>();
private List<String> MMailSubject = new ArrayList<>();
private List<String> ModifiedCoreSiteValues = new ArrayList<>();
private List<String> MultiSpinnerList = new ArrayList<>();
private String ReaderSelectedItem, CoreSitesString, modifiedcoresitestring;
private TableLayout Table;
String[] CoreSiteStringArray;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dcoreader);

Toolbar = (Toolbar) findViewById(R.id.DCOReadertoolbar);
StateText = (TextView) findViewById(R.id.ReaderState);
StateTextDesc = (TextView) findViewById(R.id.ReaderStateDescLabel);
Table = (TableLayout) findViewById(R.id.DCOReaderTable);
ReaderGo = (Button) findViewById(R.id.ReaderGo);
MultiSpinner multiSpinner = (MultiSpinner) findViewById(R.id.multi_spinner);


assert Toolbar != null;
Toolbar.setLogo(R.drawable.analyzer);
setSupportActionBar(Toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);


Bundle extras = getIntent().getExtras();
if (extras != null) {

DCOID = extras.getStringArrayList("DCOID");
ActivityDate = extras.getStringArrayList("ActivityDate");
CoreSite = extras.getStringArrayList("CoreSite");
Snags = extras.getStringArrayList("Snags");
SnagDesc = extras.getStringArrayList("SnagDesc");
Mapping = extras.getStringArrayList("Mapping");
MappingDesc = extras.getStringArrayList("MappingDesc");
Layout = extras.getStringArrayList("Layout");
LayoutDesc = extras.getStringArrayList("LayoutDesc");
ReaderSelectedItem = extras.getString("ReaderSelectedItem");
MailSubject = extras.getStringArrayList("MailSubject");
}

Set<String> CoreSiteValues = new HashSet<>();
CoreSiteValues.addAll(CoreSite);
ModifiedCoreSiteValues.addAll(CoreSiteValues);
CoreSitesString = ModifiedCoreSiteValues.toString();
CoreSitesString = CoreSitesString.replaceAll(",", "\",\"");
modifiedcoresitestring = CoreSitesString.replaceAll("]", "\"]");
modifiedcoresitestring = modifiedcoresitestring.replaceAll("\\[", "[\"");


multiSpinner.setItems(MultiSpinnerList, modifiedcoresitestring,this);

}

}


XML Spinner part:

<esmviewer.myandroid.com.esmviewer.MultiSpinner
android:id="@+id/multi_spinner"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
/>

Answer

ohhh, The small and miner mistake you've done is you forgot to implement the interface MultiSpinnerListener. Your DCOReader Activity should be like this,

public class DCOReader extends AppCompatActivity implements MultiSpinner.MultiSpinnerListener{
  ....
}