微调器删除项目

发布于 2024-11-07 03:38:21 字数 3746 浏览 0 评论 0原文

我开发了一个应用程序,其中我在旋转器中输入编辑文本的一个编辑文本值,单击按钮,我选择删除的项目被删除,但问题是,当我删除旋转器的最后一个项目时,整个旋转器列表都会刷新。我只想删除选定的项目(仅最后一个项目)

我的代码如下:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

超可爱的懒熊 2024-11-14 03:38:21

要删除所有项目,请使用:

mySpinner.setAdapter(null);

如果要删除特定项目,请将其从 m_adapterForSpinner 中删除,然后:

mySpinner.setAdapter(m_adapterForSpinner);

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);
喵星人汪星人 2024-11-14 03:38:21

对我来说,接受的答案抛出了一个异常,所以我所做的就是这样。

    ArrayAdapter adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, Collections.emptyList());
spinner.setAdapter(adapter);

For me the accepted answer throwed an exception so what I did was this.

    ArrayAdapter adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, Collections.emptyList());
spinner.setAdapter(adapter);
拔了角的鹿 2024-11-14 03:38:21

请记住:“remove”和“clear”方法不适用于 Android 2.3.3 及更低版本的 ArrayAdapter。您将收到错误消息“不支持的操作.... bla bla bla”。我不知道,可能它们适用于 Android 4.0 及更高版本。
这是我的解决方案,即使使用 minSdkVersion="5" 也可以使用。

  1. 创建 Spinner 并用测试数据填充它
    该代码采用活动形式。使用ArrayList代替数组作为ArrayAdapter的数据源。这很重要,因为稍后您将使用其“删除”方法

    列表<字符串> reportFiles = new ArrayList();        
    //填充一些测试数据     
    reportFiles.add("aaaaaaaaaaaaa");
    reportFiles.add("bbbbbbbbbbbbbb");
    reportFiles.add("cccccccccccccc");
    
    //从相应的布局xml文件创建微调器
    Spinner spnReport = (Spinner) findViewById(R.id.spinner_report);
    
    //创建ArrayAdapter
    ArrayAdapter 报告适配器 = 新的 ArrayAdapter(这个, 
            android.R.layout.simple_spinner_dropdown_item,reportFiles);
    
    //将适配器设置为微调器
    spnReport.setAdapter(reportAdapter);
    
    //选择适配器中的第一项 - 不是必需的
    spnReport.setSelection(0);
    
  2. 从微调器中删除所选项目

    reportFiles.remove((String)spnReport.getSelectedItem());

    //使用修改后的ArrayList创建新的ArrayAdapter
    reportAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,reportFiles);

    //将新的数组适配器设置为微调器
    spnReport.setAdapter(reportAdapter);

    //选择第一项
    spnReport.setSelection(0);

Remember: methods "remove" and "clear" do not work with ArrayAdapter for Android 2.3.3 and lower. You'll receive error message "Unsupported operation.... bla bla bla". May be they work with Android 4.0 and up, I don't know.
Here is my solution which works even with minSdkVersion="5".

  1. Create Spinner and fill it with test data
    This code is in an activity form. Use ArrayList as data source for ArrayAdapter instead of array. It's important because you'll use its "remove" method later

    List<String> reportFiles = new ArrayList<String>();        
    //fill some test data     
    reportFiles.add("aaaaaaaaaaaaa");
    reportFiles.add("bbbbbbbbbbbbb");
    reportFiles.add("ccccccccccccc");
    
    //create spinner from corresponding layout xml file
    Spinner spnReport  = (Spinner) findViewById(R.id.spinner_report);
    
    //create ArrayAdapter
    ArrayAdapter reportAdapter =  new ArrayAdapter(this, 
            android.R.layout.simple_spinner_dropdown_item,  reportFiles);
    
    //set adapter to spinner
    spnReport.setAdapter(reportAdapter);
    
    //select first item in adapter - is not obligatory
    spnReport.setSelection(0);
    
  2. Remove selected item from Spinner

    reportFiles.remove((String)spnReport.getSelectedItem());

    //create new ArrayAdapter with modified ArrayList
    reportAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,reportFiles);

    //set new array adapter to spinner
    spnReport.setAdapter(reportAdapter);

    //select first item
    spnReport.setSelection(0);

兔姬 2024-11-14 03:38:21

我也有同样的问题。这样做确实解决了问题,通过清空旋转器:

mySpinner.setSelection(Adapter.NO_SELECTION);

I had the same problem. Doing this did solve the problem, by emptying the spinner:

mySpinner.setSelection(Adapter.NO_SELECTION);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文