2011-05-13 195 views
5

我開發了一個應用程序,其中編輯文本的編輯文本值是在按鈕上的微調器中輸入的,點擊了我選擇要刪除的項目被刪除,但問題是,當我刪除最後一項微調整個微調列表衝。我想刪除只選定項目(僅最後一個項目)Spinner刪除項目

我的代碼如下:

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

@Shruthi如果你點擊刪除按鈕,選定的值應該被刪除,但現在所有的值都被刪除了嗎? – Venky 2011-05-13 08:39:59

回答

7

用於刪除所有項目使用:

mySpinner.setAdapter(null); 

,如果你想刪除特定項目,請將其從m_adapterForSpinner中刪除,然後:

mySpinner.setAdapter(m_adapterForSpinner); 
+11

在我的情況下,mySpinner.setAdapter(null); 引發異常! – 2012-07-12 11:21:32

+0

@thenosic將適配器設置爲null將刪除微調器和適配器之間的連接,實際上我們試圖只刪除適配器項 – SoliQuiD 2014-09-28 09:54:12

-2

我遇到了同樣的問題。這樣做解決了問題,通過清空微調:

mySpinner.setSelection(Adapter.NO_SELECTION); 
0

記住:方法「刪除」和「清除」不ArrayAdapter爲Android 2.3.3,下工作。您將收到錯誤消息「不支持的操作.... bla bla bla」。可能他們使用Android 4.0以上,我不知道。 這是我的解決方案,即使在minSdkVersion =「5」的情況下也能正常工作。

  1. 創建微調,並用測試數據填充它 此代碼是在活動形式。使用ArrayList作爲ArrayAdapter的數據源而不是數組。因爲你可以使用它的 「刪除」 方法後

    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. 刪除所選項目從微調

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

  3. 這一點很重要
0

對我來說,接受的答案拋出了異常,所以我所做的就是這樣。

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