Android Spinner有多个选择

时间:2016-05-30 16:53:41

标签: java android spinner

我是Android的新手,我一整天都在尝试使用以下代码中的多个选项来应用微调器(在此问题上接受了答案Android Spinner with multiple choice

问题是我无法通过以下行将侦听器从主活动传递到微调器Java文件:

multiSpinner.setItems(MultiSpinnerList, modifiedcoresitestring, this);

参数(this)是不可接受的,我也试过MainActivity.this但没有成功。这有什么问题?

spinner代码中的Set方法应该接收(监听器),但我不知道它是什么以及如何发送它。

微调器Java代码:

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);
}
}

活动代码:

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部分:

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

1 个答案:

答案 0 :(得分:2)

哦,你所犯的小错误是你忘了实现界面MultiSpinnerListener。您的DCOReader活动应该是这样的,

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