根据条件选择微调器值

时间:2012-04-17 11:43:30

标签: android spinner

我有以下代码:

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

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

1 个答案:

答案 0 :(得分: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();