Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner delete items

I have developed an application in which i have one edit text value of edit text is entered in spinner on button clicked an the item i select to delete gets deleted but the problem is that when i delete the last item of spinner whole spinner list flushes . i want to delete only selected item(only last item)

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 (in the case of 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);
    // m_adapterForSpinner.add("dummy item");

    // //////////////////////////////////////////////////////////////
    // 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() {
    Log.v("TAG", "addNewSpinnerItems called");

    if (m_addItemText.getText().length() == 0) {// ||(m_addItemText.getText().toString()==
        // " ")){
        Log.v("In if cndtn", "textHolder is of length 0");
        Toast.makeText(getApplicationContext(), "The textView is empty",
                Toast.LENGTH_LONG).show();
        //m_myDynamicSpinner.clearFocus();
        //m_myDynamicSpinner.setFocusable(false);
        //m_addItemText.setFocusable(true);
    } else {
        CharSequence textHolder = "" + m_addItemText.getText();
        // else{
        Log.v("else", textHolder + "");
        // m_adapterForSpinner.add(textHolder);
        // }
        ((ArrayAdapter) m_adapterForSpinner).add(textHolder);
    }
    m_addItemText.setText("");
}

private void clearSpinnerItems() {
    // m_adapterForSpinner.clear();
    Log.v("TAG", "clearSpinnerItems called");
    m_myDynamicSpinner
            .setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> parent,
                        View view, int pos, long id) {
                    // TODO Auto-generated method stub
                    Log.v("TAG", "Itemseleted to be removed is "
                            + m_adapterForSpinner.getItem(pos).toString());
                    Log.v("Position of item", pos + "");
                    Object t = m_adapterForSpinner.getItem(pos);
                    Log.v("Object t ", (String) t);

                    ((ArrayAdapter) m_adapterForSpinner).remove((CharSequence) t);
                    Log.v("Item removed", t + "");

                    // m_myDynamicSpinner.setSelected(false);
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }
            });
    // m_adapterForSpinner.add("dummy item");
}
like image 931
Shruti Avatar asked May 13 '11 07:05

Shruti


1 Answers

for delete all items use:

mySpinner.setAdapter(null);

if you want to delete a particular item, remove this from m_adapterForSpinner and then:

mySpinner.setAdapter(m_adapterForSpinner);
like image 160
thenosic Avatar answered Oct 06 '22 07:10

thenosic