Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting last item from spinner deletes the entire list

I am trying to use a spinner control that will enable the user to delete any list element. I have an 'add' button to add elements to the list, and a 'delete' button that removes the currently-displayed item from the list.

It works as expected except when the user deletes the last item in the list. At that point, all of the list's items are deleted.

My code is as follows:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    // grab our UI elements so we can manipulate them (for the Spinner)
    // or add listeners to them (in the case of the buttons)
    m_myDynamicSpinner = (Spinner)findViewById(R.id.dynamicSpinner);
    m_addItemText = (EditText)findViewById(R.id.newSpinnerItemText);
    Button addButton = (Button)findViewById(R.id.AddBtn);
    Button clearButton = (Button)findViewById(R.id.ClearBtn);

    // create an arrayAdapter an assign it to the spinner
    m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
        ((ArrayAdapter)m_adapterForSpinner).setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    m_myDynamicSpinner.setAdapter(m_adapterForSpinner);

    // add listener for addButton
    addButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            addNewSpinnerItem();
        }
    });

    clearButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            clearSpinnerItems();
        }
    });
}

// add listener for addButton
private void addNewSpinnerItem() {
    if (m_addItemText.getText().length() == 0) {
        Toast.makeText(getApplicationContext(), "The textView is empty", Toast.LENGTH_LONG).show();
    } else {
        CharSequence textHolder = "" + m_addItemText.getText();
        ((ArrayAdapter) m_adapterForSpinner).add(textHolder);
    }
    m_addItemText.setText("");
}

private void clearSpinnerItems() {
    m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            Object t = m_adapterForSpinner.getItem(pos);
            ((ArrayAdapter) m_adapterForSpinner).remove((CharSequence) t);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO
        }
    });
}

Does anyone have any ideas or suggestions on how to make this work?

like image 971
Shruti Avatar asked May 11 '11 04:05

Shruti


1 Answers

The problem with your code is that the deletion is inside the onItemSelected callback, which gets called every time you are deleting an entry, thus deleting recursively until you effectively do not have any more entries to select. If you add a log inside that method:

Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());

you will see what I mean. I'm sure you can come up with more elegant code, but a quick and dirty hack is to set up a boolean flag to stop the recursion after the first deletion. See the snippet below and add the commented lines to your own code:

public class SpinnerTest extends Activity {
    Spinner m_myDynamicSpinner;
    EditText m_addItemText;
    ArrayAdapter m_adapterForSpinner;

    public static boolean cleared = false;  //   <--- set up a static boolean here

       @Override
        public void onCreate(Bundle savedInstanceState) {
// all your code unchanged

        clearButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                cleared=false;   //   <--- nope, we did not clear the value yet

                clearSpinnerItems();
            }
        });
    }

// code unchanged

    private void clearSpinnerItems() {
        m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                Object t = m_adapterForSpinner.getItem(pos);
                Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());

                if (!cleared)  //   <--- did I do it already?

                    ((ArrayAdapter) m_adapterForSpinner).remove((CharSequence) t);

                Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());

                cleared=true;  // I did it!

            }

// code unchanged
like image 67
Aleadam Avatar answered Sep 30 '22 14:09

Aleadam