我在Iterator上有一个java.lang.UnsupportedOperationException
。删除()但是我认为这被认为是safe
我认为我的代码实际上是“好的”所以有人可以帮助我吗?
final List<String> liveList = Arrays.asList((String[]) button.getItems().toArray()); //create mutable List<String> from button.getItems() List<String>
final PremSpinner dropdown = new PremSpinner(this); //spinner (EG: "dropdown") of all the options
dropdown.setLayoutParams(col);
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, liveList); // <---+
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // |
dropdown.setPadding(45, 15, 45, 15); // |
dropdown.setAdapter(dataAdapter); //the way you [populate] a `dropdown(spinner)` is via an `adapter` and a List<String> |
final EditText partSearcher = new EditText(this);
partSearcher.setLayoutParams(col);
partSearcher.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Log.w("typing",s.toString());
Iterator<String> iter = liveList.iterator();
while (iter.hasNext()) {
if (!iter.next().contains(s)) {
iter.remove(); //<--the UnsupportedOperationException is here
}
}
dataAdapter.notifyDataSetChanged();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
然后我将它们添加到table
tr = new TableRow(this);
tr.setLayoutParams(lp);
tr.addView(partSearcher);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tr = new TableRow(this);
tr.setLayoutParams(lp);
tr.addView(dropdown);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); //this makes it fat enough
代码是为那些想要在他们的微调器中进行文本搜索的人设计的,对动态微调器有很多兴趣,但没有可靠的可行代码示例,所以我希望将这个作为博客文章,但是我无法理解!
答案 0 :(得分:0)
在这种情况下,迭代器基于List,它不支持remove();
您需要传入要删除的对象的实际对象或索引。
答案 1 :(得分:0)
好的繁荣我将完全回答这个问题。代码是合理的,这是关于immutable nature of the Arrays.asList
created secretly as part of the hidden createFromResource()
constructor call的一个小问题。
这是一个可搜索/文本过滤的微调器(又名:dropdown或selectbox或combobox),适用于使用Java编写的Android。它在程序上是灵活的(不依赖于XML - 所以它非常适合dialog
,这是我用它作为即时弹出窗口的地方)
//assuming that getItems() is a List<String> of your Spinner contents
final List<String> liveList = Arrays.asList((String[]) getItems().toArray()); //create mutable List<String> from getItems() List<String>
final Spinner dropdown = new Spinner(this); //spinner (EG: "dropdown") of all the options
dropdown.setLayoutParams(col);
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new ArrayList<String>()); // <---+
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // |
dropdown.setPadding(45, 15, 45, 15); // |
dropdown.setAdapter(dataAdapter); //the way you [populate] a `dropdown(spinner)` is no longer via a List<String> |
Iterator<String> iter = liveList.iterator();
while (iter.hasNext()) {
dataAdapter.add(iter.next());
} //instead of using the constructor we [populate] them "dynamically" directly into the ArrayAdapter
final EditText partSearcher = new EditText(this);
partSearcher.setLayoutParams(col);
partSearcher.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Log.w("typing",s.toString());
List<String> keepList = new ArrayList<String>();
Iterator<String> iter = liveList.iterator();
while (iter.hasNext()) {
String menow = iter.next();
if (menow.toLowerCase().contains(s.toString().toLowerCase())) {
keepList.add(menow);
}
}
dataAdapter.clear();
dataAdapter.addAll(keepList);
dataAdapter.notifyDataSetChanged();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
然后,您可以将其动态添加到table
,如原始问题的第二个代码块所示
请注意,有些setLayoutParams
来电没有解释 - 这些仅仅是针对我自己需要的良好做法,可以省略