簡體   English   中英

可搜尋的微調器無法在Android Java中使用

[英]searchable spinner not working in Android Java

我在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

該代碼被設計為希望在微調器中進行文本搜索的人員可移植,動態微調器引起了很多興趣,但沒有可靠的可行代碼示例,因此我希望將其作為博客文章,但我可以弄清楚!

在這種情況下,迭代器基於不支持remove()的List。

您將需要傳遞實際對象或要刪除的對象的索引。

好的,我要充分回答這個問題。 代碼聽起來Arrays.asList這是一個關於Arrays.asList不可變性質的小問題,它是作為createFromResource()隱藏構造函數調用的一部分秘密創建的

這是適用於以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調用沒有解釋-這些只是作為我自己需要的一種好習慣,可以省略

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM