0

我有以下代码:

private void spinnerValue(){
    spinnerwho=(Spinner)findViewById(R.id.spinnerWho);
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    adapter.add("A");
    adapter.add("B");
    spinnerwho.setAdapter(adapter);

    spinnerwho.setOnItemSelectedListener(spinnerlistener);
}
private OnItemSelectedListener spinnerlistener= new OnItemSelectedListener(){
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        if(arg0.getSelectedItem().toString().contentEquals("A")){
            if (imagechanged){
                displayalert();
            }
            else{
                // Do some operation
            }
        }

        if(arg0.getSelectedItem().toString().contentEquals("B")){
            // do some operation            
        }
    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }
};

private void displayAlert(DialogInterface.OnClickListener listener) {
    AlertDialog dlgAlertBox;
    AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
    dlgAlert.setMessage("data will be lost if you change value without saving. Continue?");
    dlgAlert.setPositiveButton("Yes", listener);
    dlgAlert.setNegativeButton("No", listener);
    dlgAlert.setCancelable(true);           
    dlgAlertBox = dlgAlert.create();
    dlgAlertBox.show();
}
protected DialogInterface.OnClickListener BackButtonListenerforengineer = new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) 
    {
        switch (which){
        case DialogInterface.BUTTON_POSITIVE:
            // do some operation
            break;
        case DialogInterface.BUTTON_NEGATIVE:
            dialog.dismiss();
// revert to previous selection
            /*Signature.this.spinnerwho.getAdapter();
            ArrayAdapter<String> adapter=new ArrayAdapter<String>(Signature.this, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            adapter.clear();
            adapter.add("Tenant");
            adapter.add("Engineer");
            spinnerwho.setAdapter(adapter);
            spinnerwho.setSelection(1,true);

            spinnerwho.setOnItemSelectedListener(null);*/


            break;
    }

我选择微调器的值并检查条件。当我尝试更改微调器项目时,它会显示带有是和否按钮的警报对话框。单击“是”将更改微调器的值。如果用户单击“否”,则它应该将微调器恢复为先前的选择。我尝试清除微调器适配器并用新值填充它。但不工作。任何人都可以通过指出我哪里出错来帮助我吗?提前致谢。

4

1 回答 1

0

请遵循以下方法来使用微调器值。

private void addListenerOnSubjectSpinner() 
{
    spiSubject.setOnItemSelectedListener(new mySpinnerListener());      
}



class mySpinnerListener implements Spinner.OnItemSelectedListener
{
    @Override
    public void onItemSelected(AdapterView parent, View v, int position,
            long id) 
    {
        Toast.makeText(parent.getContext(), "Subject is " +
                  parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
        owq.setSubjectName(parent.getItemAtPosition(position).toString());
    }

    @Override
    public void onNothingSelected(AdapterView parent) 
    {
        // Do nothing.
    }

}

在 onCreate() 中调用这两个方法。

addListenerOnSubjectSpinner();
于 2012-04-17T11:49:47.547 回答