微调器删除项目
我开发了一个应用程序,其中我在旋转器中输入编辑文本的一个编辑文本值,单击按钮,我选择删除的项目被删除,但问题是,当我删除旋转器的最后一个项目时,整个旋转器列表都会刷新。我只想删除选定的项目(仅最后一个项目)
我的代码如下:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要删除所有项目,请使用:
如果要删除特定项目,请将其从 m_adapterForSpinner 中删除,然后:
for delete all items use:
if you want to delete a particular item, remove this from m_adapterForSpinner and then:
对我来说,接受的答案抛出了一个异常,所以我所做的就是这样。
For me the accepted answer throwed an exception so what I did was this.
请记住:“remove”和“clear”方法不适用于 Android 2.3.3 及更低版本的 ArrayAdapter。您将收到错误消息“不支持的操作.... bla bla bla”。我不知道,可能它们适用于 Android 4.0 及更高版本。
这是我的解决方案,即使使用 minSdkVersion="5" 也可以使用。
创建 Spinner 并用测试数据填充它
该代码采用活动形式。使用ArrayList代替数组作为ArrayAdapter的数据源。这很重要,因为稍后您将使用其“删除”方法
从微调器中删除所选项目
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".
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
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);
我也有同样的问题。这样做确实解决了问题,通过清空旋转器:
I had the same problem. Doing this did solve the problem, by emptying the spinner: