0

我的 Iterator .remove() 有一个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

该代码旨在为希望在其微调器中进行文本搜索的人移植,人们对动态微调器很感兴趣,但没有可靠的可行代码示例,所以我希望将这篇文章写成一篇博客文章,但我可以'想不通!

4

2 回答 2

0

本例中的迭代器基于不支持 remove() 的 List;

您需要传入实际对象或要删除的对象的索引。

于 2013-09-03T16:26:44.003 回答
0

好的,为了繁荣,我将完全回答这个问题。该代码是合理的,它是关于作为隐藏构造函数调用的一部分秘密创建的不可变性质的Arrays.asListcreateFromResource()一个小问题。

这是一个用 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) {}
});

然后,您可以将它们动态添加到 atable中,如原始问题的第二个代码块中所示

请注意,有些setLayoutParams电话没有解释 - 这些只是为了满足我自己的需要,可以省略

于 2013-09-04T10:41:29.343 回答