Spinner删除项目

时间:2011-05-13 07:06:45

标签: android listview spinner adapter

我开发了一个应用程序,其中我有一个编辑文本值的编辑文本在微调器上输入按钮点击一个我选择删除的项目被删除但问题是,当我删除微调器整个微调器的最后一项列表刷新。我想只删除所选项目(仅限最后一项)

我的代码如下:

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

4 个答案:

答案 0 :(得分:9)

删除所有项目使用:

mySpinner.setAdapter(null);

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

mySpinner.setAdapter(m_adapterForSpinner);

答案 1 :(得分:0)

记住:方法&#34;删除&#34;和&#34;清除&#34;不适用于适用于Android 2.3.3及更低版本的ArrayAdapter。您将收到错误消息&#34;不支持的操作.... bla bla bla&#34;。可能是他们使用Android 4.0及以上,我不知道。 这是我的解决方案,即使使用minSdkVersion =&#34; 5&#34;。

  1. 创建微调器并用测试数据填充它 此代码采用活动形式。使用ArrayList作为ArrayAdapter的数据源而不是数组。这很重要,因为您将使用它&#34;删除&#34;方法稍后

    <form action="<? echo $_SERVER['PHP_SELF']; ?>" id="contact-form" method="post" class="form afsana-form" role="form">
                                <div class="row">
                                    <div class="col-sm-12 form-group">
                                        <input class="form-control afsana-style" id="name" name="name" placeholder="name" type="text" required autofocus />
                                    </div>
                                    <div class="col-sm-12 form-group">
                                        <input class="form-control afsana-style" id="email" name="email" placeholder="email" type="email" required />
                                    </div>
                                    <div class="col-sm-12 form-group">
                                        <textarea class="form-control" id="message" name="message" placeholder="message" rows="5"></textarea>
                                    </div>
                                    <div class="col-sm-12 form-group">
                                        <button class="btn btn-primary afsana-btn" name="submit" value="verzenden" type="submit">Verzenden <i class="ion-arrow-graph-up-right"></i></button>
                                    </div>
                                </div>
                            </form>
                            <?php
    
    if(isset($_POST["submit"])){
    // Checking For Blank Fields..
    if($_POST["name"]==""||$_POST["email"]==""||$_POST["message"]==""){
    echo "Fill All Fields..";
    }else{
    // Check if the "Sender's Email" input field is filled out
    $email=$_POST['email'];
    // Sanitize E-mail Address
    $email =filter_var($email, FILTER_SANITIZE_EMAIL);
    // Validate E-mail Address
    $email= filter_var($email, FILTER_VALIDATE_EMAIL);
    if (!$email){
    echo "Invalid Sender's Email";
    }
    else{
    $subject = (Contact_form);
    $message = $_POST['message'];
    $headers = 'From:'. $email . "\r\n"; // Sender's Email
    $headers .= 'Cc:'. $email2 . "\r\n"; // Carbon copy to Sender
    // Message lines should not exceed 70 characters (PHP rule), so wrap it
    $message = wordwrap($message, 70);
    // Send Mail By PHP Mail Function
    mail("something@domain.com", $subject, $message, $headers);
    echo "Your mail has been sent successfuly ! Thank you for your feedback";
    }
    }
    }
    ?>
    
  2. 从Spinner中删除所选项目

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

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

答案 2 :(得分:0)

对我而言,接受的答案引发了异常,所以我所做的就是这个。

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

答案 3 :(得分:-2)

我遇到了同样的问题。这样做可以通过清空微调器来解决问题:

mySpinner.setSelection(Adapter.NO_SELECTION);