小编典典

Switch语句仅返回最后一种情况

java

切换语句修复:
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);
}

}


阅读 423

收藏
2020-11-26

共1个答案

小编典典

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

2020-11-26