2012-10-05 74 views
0

因此,我設置了一個SimpleAdapter,我想在它上面添加一個editText,您可以輸入搜索查詢並實時更新列表。EditText在Simpleadapter上搜索過濾器?

我知道我已經可以setTextFilterEnabled(true)並強制彈出鍵盤。當你按下editText並且完成它時,我會這麼做。

我遇到的問題是,當你搜索時彈出的字母是非常醜​​陋和太大。它佔據了屏幕的四分之一,涵蓋了搜索結果。有沒有一種方法來定製這個?在這種情況下,有沒有辦法在simpleAdapter中使用editText,這樣我就可以刪除那個盒子了?

回答

0

雖然Tobias的回答很有洞察力,但它沒有回答我的問題,而且對於我需要做的事情來說相當複雜。我不是每次都清理並重新加載列表,而是選擇在陣列適配器中使用Filter來自動執行所有這些操作:

This is the article幫助我做到了這一點,而且非常有用。

0

您必須將addTextChangedListener加上TextWatcher

Boolean[] isPresent; 

ed = (EditText) findViewById(R.id.editText1); 
    ed.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void afterTextChanged(Editable arg0) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void beforeTextChanged(CharSequence arg0, int arg1, 
        int arg2, int arg3) { 

      } 

      @Override 
      public void onTextChanged(CharSequence arg0, int arg1, int arg2, 
        int arg3) { 

       textLength = ed.getText().length(); 
       list.clear(); 
       isPresent = initIsPresentArray(); 

       for (int i = 0; i < tempList.length; i++) { 
        for (int j = 0; j < (tempList[i].length() - (textLength - 1)); j++) { 
         if (textLength <= tempList[i].length()) { 
          if (isPresent[i] == false) { 
           if (ed.getText() 
             .toString() 
             .equalsIgnoreCase(
               (String) tempList[i] 
                 .subSequence(
                   j, 
                   (j + textLength)))) { 
            list.add(tempCaseList.get(i)); 
            isPresent[i] = true; 
           } 
          } 
         } 
        } 
        if (list.size() == 0) { 
         error.setText(getResources().getString(R.string.notFound)); 
        } else { 
         error.setText(""); 
        } 

        lv.setAdapter(new CustomAdapter(History.this, list)); 
       } 
      } 
     }); 

而且方法initIsPresentArray()

public Boolean[] initIsPresentArray() { 
      Boolean[] isPresent = new Boolean[tempCaseList.size()]; 
      for (int i = 0; i < isPresent.length; i++) { 
       isPresent[i] = false; 
      } 
      return isPresent; 
     } 

正如你可能會看到,我不做直接包含的信息ArrayList操作,但我在複製這個ArrayList到一個臨時ArrayList,使用這種方法

public ArrayList<Case> createTempList(ArrayList<Case> listOfCases) { 

    ArrayList<Case> temporaryList = new ArrayList<Case>(); 

    for (Case c : listOfCases) { 
     temporaryList.add(c); 
    } 

    return temporaryList; 
} 

只要添加評論,如果有什麼東西是未完成的!

+0

那肯定會變得很快。我會試一試,讓你知道它是怎麼回事! – VicVu

+0

是的。但它對我來說非常合適 –

+0

在上週遇到了幾個障礙。我會回到這裏,讓我知道如果它能儘快工作,對不起! – VicVu