切换语句只返回最后一个案例

时间:2013-08-01 14:41:24

标签: java android textview switch-statement

切换语句修复: switch语句仅返回最后一种情况,即情况4,“#0R0dfdf0FF”。我如何解决这个问题,以便文本视图显示在对话框中单击的那个? 我是一个全新的人,所以真的很感激你的帮助。

public class NoteEdit extends Activity {
public EditText mTitleText;
public EditText mBodyText;
public EditText mColor;
private NotesDbAdapter mDbHelper;
private static final int DIALOG_ALERT = 10;
Long mRowId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();
    setContentView(R.layout.note_edit);
    setTitle(R.string.done);
    mTitleText = (EditText) findViewById(R.id.editTitle);
    mBodyText = (EditText) findViewById(R.id.editNote);
    mColor = (EditText) findViewById(R.id.editColor);
    mRowId = (savedInstanceState == null) ? null :
        (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
    if (mRowId == null) {
        Bundle extras = getIntent().getExtras();
        mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
                                : null;
    }
    populateFields();
    setupActionBar();
}
private void setupActionBar() {

    getActionBar().setDisplayHomeAsUpEnabled(true);

}
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        setResult(RESULT_OK);
        finish();


    }
    return super.onOptionsItemSelected(item);
}
private void populateFields() {
    if (mRowId != null) {
        Cursor note = mDbHelper.fetchNote(mRowId);
        startManagingCursor(note);
        mTitleText.setText(note.getString(
                    note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
        mBodyText.setText(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
        mColor.setText(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_COLOR)));
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    saveState();
    outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
}
@Override
protected void onPause() {
    super.onPause();
    saveState();
}
@Override
protected void onResume() {
    super.onResume();
    populateFields();
}
private void saveState() {
    String title = mTitleText.getText().toString();
    String body = mBodyText.getText().toString();
    String color = mColor.getText().toString();

    if (mRowId == null) {
        long id = mDbHelper.createNote(title, body, color);
        if (id > 0) {
            mRowId = id;
        }
    } else {
        mDbHelper.updateNote(mRowId, title, body, color);
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getItemId()) {
    case R.id.add:
        showDialog(DIALOG_ALERT);
        return true;
    }

    return super.onMenuItemSelected(featureId, item);
}
@Override
protected Dialog onCreateDialog(int id) {
  switch (id) {
  case DIALOG_ALERT:
    // Create out AlterDialog
    android.app.AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final String[] colors = {"Blue", "Green", "Yellow", "Red", "Purple"};
    builder.setTitle(R.string.body);
    builder.setItems(colors, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        // The 'which' argument contains the index position
        // of the selected item
            switch (which){
            case 0:
                mColor.setText("#000000");
            case 1:
                mColor.setText("#0000FF");
            case 2:
                mColor.setText("#0R00FF");
            case 3:
                mColor.setText("#0R00dsdFF");
            case 4:
                mColor.setText("#0R0dfdf0FF");
            default:  
                break; 
            }
    } });
    AlertDialog dialog = builder.create();
    dialog.show();
  }
  return super.onCreateDialog(id);
}

}

4 个答案:

答案 0 :(得分:7)

您在交换机分支末尾缺少break;

答案 1 :(得分:4)

堕落。

您必须添加break

case 0:
          mColor.setText("#000000");
          break;

You can find that in docs

  

break语句是必要的,因为如果没有它们,switch块中的语句就会失败:匹配的case标签之后的所有语句都按顺序执行,而不管后续case标签的表达式,直到遇到break语句。

答案 2 :(得分:1)

如果您没有break,则需要return,否则会导致失败

答案 3 :(得分:0)

你需要休息;除了最后一个案件以外的所有案件,否则它将逐案审理

 switch (which){
        case 0:
            mColor.setText("#000000");
            break;        
        case 1:
            mColor.setText("#0000FF");
            break;        
        case 2:
            mColor.setText("#0R00FF");
            break; 
        case 3:
            mColor.setText("#0R00dsdFF");
            break; 
        case 4:
            mColor.setText("#0R0dfdf0FF");
        default:  
            break; 
        }