public class NewNote extends AppCompatActivity implements View.OnClickListener { EditText edNewThemeNotes, edNewTextNotes; ImageButton edLightRed, edLightYellow, edLightGreen, img_map; ImageView edAddedPhoto; DBHelper dbHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new_note); edNewThemeNotes = (EditText) findViewById(R.id.edNewThemeNotes); edNewTextNotes = (EditText) findViewById(R.id.edNewTextNotes); edLightRed = (ImageButton) findViewById(R.id.edLightRed); edLightYellow = (ImageButton) findViewById(R.id.edLightYellow); edLightGreen = (ImageButton) findViewById(R.id.edLightGreen); edAddedPhoto = (ImageView) findViewById(R.id.edAddedPhoto); dbHelper = new DBHelper(this); } @Override public boolean onCreateOptionsMenu(Menu menu){ getMenuInflater().inflate(R.menu.add_note, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item){ int id = item.getItemId(); String newTheme = edNewThemeNotes.getText().toString(); String newText = edNewTextNotes.getText().toString(); SQLiteDatabase database = dbHelper.getWritableDatabase(); ContentValues contentValues = new ContentValues(); switch (id) { case R.id.edAddNewNote: if (edNewThemeNotes.getText().toString().equals("")) { Toast error = Toast.makeText(NewNote.this, R.string.toast_not_theme, Toast.LENGTH_LONG); error.show(); } else if (edNewTextNotes.getText().toString().equals("")) { Toast error = Toast.makeText(NewNote.this, R.string.toast_not_text, Toast.LENGTH_LONG); error.show(); } else { contentValues.put(DBHelper.KEY_THEME, newTheme); contentValues.put(DBHelper.KEY_TEXT, newText); contentValues.put(DBHelper.KEY_PRIORITY, newNumPriority); database.insert(DBHelper.TABLE_NOTES, null, contentValues); } break; } return false; } @Override public void onClick(View view) { int id = view.getId(); int num; if (id == R.id.edLightRed) { num = 1; } else if (id == R.id.edLightYellow){ num = 2; } else if (id == R.id.edLightGreen){ num = 3; } else { num = 0; } } } |
1 answer
You don't need to pass anything anywhere, you can make the variable num a member of the NewNote class, not the onCLick () function (like dbHelper, etc.), in the onCLick () function, just assign it a value, and use the variable onOptionsItemSelected.
We declare:
EditText edNewThemeNotes, edNewTextNotes; ImageButton edLightRed, edLightYellow, edLightGreen, img_map; ImageView edAddedPhoto; int num; DBHelper dbHelper; We assign:
public void onClick(View view) { int id = view.getId(); if (id == R.id.edLightRed) { num = 1; } else if (id == R.id.edLightYellow){ num = 2; } else if (id == R.id.edLightGreen){ num = 3; } else { num = 0; } } |