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.setAda开发者_StackOverflow社区pter(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");
}
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);
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".
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);
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);
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);
I had the same problem. Doing this did solve the problem, by emptying the spinner:
mySpinner.setSelection(Adapter.NO_SELECTION);
精彩评论