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

时间:2023-01-15
本文介绍了Switch 语句只返回最后一种情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

切换语句修复:switch 语句只返回最后一种情况,即情况 4,#0R0dfdf0FF".我该如何解决这个问题,以便文本视图显示在对话框中单击的那个?我是一个新手,所以非常感谢您的帮助.

Switch statment fix: The switch statement is only returning the last case i.e case 4, "#0R0dfdf0FF". how can i fix this so the text view shows the the one clicked in the dialogue box? I'm a total newbie so yes help would really be appreciated.

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

}

推荐答案

你在 switch 分支的末尾缺少 break;.

You are missing break; at the end of the switch branches.

这篇关于Switch 语句只返回最后一种情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:Android sdk 18 TextView 重力不能垂直工作 下一篇:Android 上 TextView 中的多个可点击链接

相关文章

最新文章