1
\$\begingroup\$

This code is very long, and you need a separate method. How can I implement it?

I have 6 (x2) imageview buttons and 6 edittext.

public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId())
        {
        case R.id.imageViewPlusSubEdit1:
            if(editTextInputNameSubEdit1.getText().toString().trim().length() == 0)
            {
                Toast.makeText(this, "Empty text!", Toast.LENGTH_SHORT).show();
            }
            else if(items1.size() > 23)
            {
                Toast.makeText(this, "Too many elements!", Toast.LENGTH_SHORT).show();
            }
            else
            {       
                items1.add(editTextInputNameSubEdit1.getText().toString());
                adapterList1.notifyDataSetChanged(); 
                editTextInputNameSubEdit1.setText("");
            }
            break;
        case R.id.imageViewMinusSubEdit1:
            SparseBooleanArray checkedItemPositions = listViewSubEdit1.getCheckedItemPositions();
            int itemCount = listViewSubEdit1.getCount();
            for(int i = itemCount - 1; i >= 0; i--)
            {
                if(checkedItemPositions.get(i))
                {
                    adapterList1.remove(items1.get(i));
                }
            }
            checkedItemPositions.clear();
            adapterList1.notifyDataSetChanged(); 
            break;

        case R.id.imageViewPlusSubEdit2:
            if(editTextInputNameSubEdit2.getText().toString().trim().length() == 0)
            {
                Toast.makeText(this, "Empty text!", Toast.LENGTH_SHORT).show();
            }
            else if(items2.size() > 23)
            {
                Toast.makeText(this, "Too many elements!", Toast.LENGTH_SHORT).show();
            }
            else
            {       
                items2.add(editTextInputNameSubEdit2.getText().toString());
                adapterList2.notifyDataSetChanged(); 
                editTextInputNameSubEdit2.setText("");
            }
            break;
        case R.id.imageViewMinusSubEdit2:
            SparseBooleanArray checkedItemPositions2 = listViewSubEdit2.getCheckedItemPositions();
            int itemCount2 = listViewSubEdit2.getCount();
            for(int i = itemCount2 - 1; i >= 0; i--)
            {
                if(checkedItemPositions2.get(i))
                {
                    adapterList2.remove(items2.get(i));
                }
            }
            checkedItemPositions2.clear();
            adapterList2.notifyDataSetChanged(); 
            break;

        case R.id.imageViewPlusSubEdit3:
            if(editTextInputNameSubEdit3.getText().toString().trim().length() == 0)
            {
                Toast.makeText(this, "Empty text!", Toast.LENGTH_SHORT).show();
            }
            else if(items3.size() > 23)
            {
                Toast.makeText(this, "Too many elements!", Toast.LENGTH_SHORT).show();
            }
            else
            {       
                items3.add(editTextInputNameSubEdit3.getText().toString());
                adapterList3.notifyDataSetChanged(); 
                editTextInputNameSubEdit3.setText("");
            }
            break;
        case R.id.imageViewMinusSubEdit3:
            SparseBooleanArray checkedItemPositions3 = listViewSubEdit3.getCheckedItemPositions();
            int itemCount3 = listViewSubEdit3.getCount();
            for(int i = itemCount3 - 1; i >= 0; i--)
            {
                if(checkedItemPositions3.get(i))
                {
                    adapterList3.remove(items3.get(i));
                }
            }
            checkedItemPositions3.clear();
            adapterList3.notifyDataSetChanged(); 
            break;

        case R.id.imageViewPlusSubEdit4:
            if(editTextInputNameSubEdit4.getText().toString().trim().length() == 0)
            {
                Toast.makeText(this, "Empty text!", Toast.LENGTH_SHORT).show();
            }
            else if(items4.size() > 23)
            {
                Toast.makeText(this, "Too many elements!", Toast.LENGTH_SHORT).show();
            }
            else
            {       
                items4.add(editTextInputNameSubEdit4.getText().toString());
                adapterList4.notifyDataSetChanged(); 
                editTextInputNameSubEdit4.setText("");
            }
            break;
        case R.id.imageViewMinusSubEdit4:
            SparseBooleanArray checkedItemPositions4 = listViewSubEdit4.getCheckedItemPositions();
            int itemCount4 = listViewSubEdit4.getCount();
            for(int i = itemCount4 - 1; i >= 0; i--)
            {
                if(checkedItemPositions4.get(i))
                {
                    adapterList4.remove(items4.get(i));
                }
            }
            checkedItemPositions4.clear();
            adapterList4.notifyDataSetChanged(); 
            break;

        case R.id.imageViewPlusSubEdit5:
            if(editTextInputNameSubEdit5.getText().toString().trim().length() == 0)
            {
                Toast.makeText(this, "Empty text!", Toast.LENGTH_SHORT).show();
            }
            else if(items5.size() > 23)
            {
                Toast.makeText(this, "Too many elements!", Toast.LENGTH_SHORT).show();
            }
            else
            {       
                items5.add(editTextInputNameSubEdit5.getText().toString());
                adapterList5.notifyDataSetChanged(); 
                editTextInputNameSubEdit5.setText("");
            }
            break;
        case R.id.imageViewMinusSubEdit5:
            SparseBooleanArray checkedItemPositions5 = listViewSubEdit5.getCheckedItemPositions();
            int itemCount5 = listViewSubEdit5.getCount();
            for(int i = itemCount5 - 1; i >= 0; i--)
            {
                if(checkedItemPositions5.get(i))
                {
                    adapterList5.remove(items5.get(i));
                }
            }
            checkedItemPositions5.clear();
            adapterList5.notifyDataSetChanged(); 
            break;

        case R.id.imageViewPlusSubEdit6:
            if(editTextInputNameSubEdit6.getText().toString().trim().length() == 0)
            {
                Toast.makeText(this, "Empty text!", Toast.LENGTH_SHORT).show();
            }
            else if(items6.size() > 23)
            {
                Toast.makeText(this, "Too many elements!", Toast.LENGTH_SHORT).show();
            }
            else
            {       
                items6.add(editTextInputNameSubEdit6.getText().toString());
                adapterList6.notifyDataSetChanged(); 
                editTextInputNameSubEdit6.setText("");
            }
            break;
        case R.id.imageViewMinusSubEdit6:
            SparseBooleanArray checkedItemPositions6 = listViewSubEdit6.getCheckedItemPositions();
            int itemCount6 = listViewSubEdit6.getCount();
            for(int i = itemCount6 - 1; i >= 0; i--)
            {
                if(checkedItemPositions6.get(i))
                {
                    adapterList6.remove(items6.get(i));
                }
            }
            checkedItemPositions6.clear();
            adapterList6.notifyDataSetChanged(); 
            break;
        }
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Step #1: create a function for the body of each case in your switch statement

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId())
    {
        case R.id.imageViewPlusSubEdit1:
            handleImageViewPlusSubEdit1();
            break;
        case R.id.imageViewMinusSubEdit1:
            handleImageViewMinusSubEdit1();
            break;
        ...
    }
}

Step #2: create your own click handler interface - or re-use the existing one

interface ClickHandler {
    void onClick();
}

Step #3: create specific classes for each of the different ways that you handle the clicks. For example:

class PlusClickHander implements ClickHandler {
    public void onClick() {
        handleImageViewPlusSubEdit1();
    }
}

Step #4: replace the switch statements that directly call the functions, with calls to the ClickHandlers

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId())
    {
        case R.id.imageViewPlusSubEdit1:
            ClickHandler plusClickHandler = new PlusClickHandler();
            plusClickHandler.onClick();
            break;
        case R.id.imageViewMinusSubEdit1:
            ClickHandler minusClickHandler = new MinusClickHandler();
            minusClickHandler.onClick();
            break;
        ...
    }
}

Step #5: move the click handlers into a Map, so that you can look them up.

public void onClick(View v) {
    Map<Id,ClickHandler> handlers = new HashMap<Id,ClickHandler>();
    handlers.put(R.id.imageViewPlusSubEdit1, new PlusClickHandler();
    handlers.put(R.id.imageViewMinusSubEdit1, new MinusClickHandler();
    ...

    ClickHandler clickHandler = handlers.get(v.getId());
    clickHandler.onClick();
}

Step #6: create a new function to get the map you need

public Map<Id,ClickHandler> createHandlers() {

    Map<Id,ClickHandler> handlers = new HashMap<Id,ClickHandler>();
    handlers.put(R.id.imageViewPlusSubEdit1, new PlusClickHandler();
    handlers.put(R.id.imageViewMinusSubEdit1, new MinusClickHandler();
    ...
    return handlers;
}

public void onClick(View v) {
    Map<Id,ClickHandler> handlers = createHandlers();

    ClickHandler clickHandler = handlers.get(v.getId());
    clickHandler.onClick();
}

Here's an example of what PlusClickHandler might look like...

public class PlusClickHandler implements ClickHandler {
    private final ? editTextInput;
    private final ? items;
    private final ? adapterList;

    private final int itemLimit = 23;

    public PlusClickHandler(...) {
        editTextInput = ?
        items = ?
        adapterList = ?
    }

    public void onClick() {

        String input = editTextInput.getText().toString();

        if(input.isEmpty())
        {
            Toast.makeText(this, "Empty text!", Toast.LENGTH_SHORT).show();
        }
        else if(items.size() > itemLimit)
        {
            Toast.makeText(this, "Too many elements!", Toast.LENGTH_SHORT).show();
        }
        else
        {       
            items.add(input);
            adapterList.notifyDataSetChanged(); 
            editTextInput.setText("");
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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