gpt4 book ai didi

android - 从微调器中删除最后一项会删除整个列表

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:39:47 25 4
gpt4 key购买 nike

我正在尝试使用微调器控件,使用户能够删除任何列表元素。我有一个“添加”按钮可以将元素添加到列表中,还有一个“删除”按钮可以从列表中删除当前显示的项目。

它按预期工作除了当用户删除列表中的最后一项时。届时,列表中的所有项目都将被删除。

我的代码如下:

    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// grab our UI elements so we can manipulate them (for 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);

// 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() {
if (m_addItemText.getText().length() == 0) {
Toast.makeText(getApplicationContext(), "The textView is empty", Toast.LENGTH_LONG).show();
} else {
CharSequence textHolder = "" + m_addItemText.getText();
((ArrayAdapter) m_adapterForSpinner).add(textHolder);
}
m_addItemText.setText("");
}

private void clearSpinnerItems() {
m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object t = m_adapterForSpinner.getItem(pos);
((ArrayAdapter) m_adapterForSpinner).remove((CharSequence) t);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO
}
});
}

有人对如何使这项工作有任何想法或建议吗?

最佳答案

您的代码的问题在于删除是在 onItemSelected 回调中进行的,每次您删除条目时都会调用该回调,从而递归删除,直到您实际上没有更多条目可供选择为止。如果您在该方法中添加日志:

Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());

你会明白我的意思。我相信您可以想出更优雅的代码,但一个快速而肮脏的 hack 是设置一个 bool 标志以在第一次删除后停止递归。请参阅下面的代码段并将注释行添加到您自己的代码中:

public class SpinnerTest extends Activity {
Spinner m_myDynamicSpinner;
EditText m_addItemText;
ArrayAdapter m_adapterForSpinner;

public static boolean cleared = false; // <--- set up a static boolean here

@Override
public void onCreate(Bundle savedInstanceState) {
// all your code unchanged

clearButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

cleared=false; // <--- nope, we did not clear the value yet

clearSpinnerItems();
}
});
}

// code unchanged

private void clearSpinnerItems() {
m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object t = m_adapterForSpinner.getItem(pos);
Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());

if (!cleared) // <--- did I do it already?

((ArrayAdapter) m_adapterForSpinner).remove((CharSequence) t);

Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());

cleared=true; // I did it!

}

// code unchanged

关于android - 从微调器中删除最后一项会删除整个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5959318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com