从微调器中删除最后一项会删除整个列表

时间:2011-05-11 04:34:12

标签: android spinner

我正在尝试使用一个微调器控件,使用户可以删除任何列表元素。 我有一个“添加”按钮,用于向列表中添加元素,还有一个“删除”按钮,用于从列表中删除当前显示的项目。

当用户删除列表中的最后一项时,它按预期之外的效果。此时,所有列表的项目都将被删除。

我的代码如下:

    @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
        }
    });
}

有没有人对如何使这项工作有任何想法或建议?

2 个答案:

答案 0 :(得分:4)

代码的问题在于删除是在onItemSelected回调中,每次删除条目时都会调用该回调,因此会递归删除,直到您实际上没有任何条目可供选择。如果在该方法中添加日志:

Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());
你会明白我的意思。我相信你可以提出更优雅的代码,但是快速而肮脏的黑客是设置一个布尔标志来在第一次删除后停止递归。请参阅下面的代码段并将注释行添加到您自己的代码中:

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

答案 1 :(得分:1)

我无法理解你的问题。你可以通过使用getSelectedItemPosition()方法获得所选项目的位置。