This is an Android app and there is a EditText
box, and I need to accept multiple types of inputs one after another in order. This order is predefined. On USER_INP_NAME
object newUser
is created. After this multiple properties are set with this object. Finally on USER_INP_STATE
the object is added to a list. This cycle repeats.
I am a "C" programmer, so this is coded in procedure-oriented manner. The switch case is looking ugly for now. Is there a better solution to do this ?
userInputs.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
int val = (Integer) userInputs.getTag();
switch (val) {
case USER_INP_NAME:
newUser = new Users();
newUser.setUsersName(userInputs.getText()
.toString());
userInputs.setTag(USER_INP_NUMBER);
userInputs.setText("");
userInputs.setHint("User Name");
break;
case USER_INP_NUMBER:
newUser.setUnitWeight(Integer
.parseInt(userInputs.getText().toString()));
userInputs.setTag(USER_INP_STATE);
userInputs.setText("");
userInputs.setHint("User Number");
break;
case USER_INP_STATE:
newUser.setShelfLife(Integer
.parseInt(userInputs.getText().toString()));
userInputs.setTag(USER_INP_NAME);
userInputs.setText("");
userInputs.setHint("User State");
uList.add(newUser);
adapter.notifyDataSetChanged();
break;
default:
break;
}
default:
break;
}
}
return false;
}
});