1
\$\begingroup\$

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;
    }
});
\$\endgroup\$
3
\$\begingroup\$

I think I would consider using three controls to achieve this and resetting their visibility on submission.

Reusing whatever you have used to create the userInputs control, rename that to nameInput and create two others weightInput and shelfLifeInput. Importantly when creating the other two controls set their visibility.

In XML:

<TextView
            android:id="@+id/weightInput"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:hint="@string/weightHint"
            android:visibility="gone" />


or programatically:

weightInput.setVisibility(View.GONE);

It is import to use View.GONE as opposed to View.HIDDEN as a HIDDEN control still takes up space in the page.

Each of your three inputs can have a handler similar to the one you have created above, but now each handler performs a slightly different function.

Name Input:

userInput.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:
                userInput.setVisibility(View.GONE);
                weightInput.setVisibility(View.VISIBLE);
                break;
            default:
                break;
            }
         }
         return false;
     }
 });

Weight Input:

weightInput.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:
                weightInput.setVisibility(View.GONE);
                shelfLifeInput.setVisibility(View.VISIBLE);
                break;
            default:
                break;
            }
         }
         return false;
     }
 });

Shelf Life Input:

shelfLifeInput.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:
                //this can probably be locally scoped now.
                //Users is an odd name for what this class appears to be.
                //assume validation will have been added.
                Users newUser = new Users();
                newUser.setUsersName(userInput.getText().toString());
                newUser.setUnitWeight(Integer.parseInt(weightInput.getText().toString()));
                newUser.setShelfLife(Integer.parseInt(shelfLifeInput.getText().toString()));
                uList.add(newUser);
                adapter.notifyDataSetChanged();
                shelfLifeInput.setVisibility(View.GONE);
                userInput.setVisibility(View.VISIBLE);

                break;
            default:
                break;
            }
         }
         return false;
     }
 });  

You would probably want to break the user creation code out into a separate function so that it is not tightly bound with the view, but for brevity I have followed more closely to your original code.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.