【问题标题】:ArrayAdapter addAll function doesn't seem to be invokedArrayAdapter addAll 函数似乎没有被调用
【发布时间】:2015-05-19 16:12:16
【问题描述】:

我下面的代码是用于列表视图的搜索过滤器。每次更改tbSearcheditText 中的文本时,都必须更改列表视图中的项目。执行在 if 语句 (txt.length()==0) 内部,但它没有添加我的数组。

public void onTextChanged(CharSequence s, int start, int before, int count) {
    String txt = tbSearch.getText().toString().toLowerCase();
    aaItems.clear();
    if (txt.length() == 0)
    {
        aaItems.addAll(arrMonth);
    }
    else {
        for (String item : arrMonth) {
            if (item.toLowerCase().contains(txt)) {
                aaItems.add(item);
            }
        }
    }
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    您传递给 ArrayAdapter 的 List 用作适配器的 支持 ArrayList。

    当你调用 clear() 时,你实际上是在清除你给它的支持数组。

    您需要确保您提供给适配器的列表与您的 arrMonth 列表不同,如下所示:

    aaItems = new ArrayAdapter(this, android.R.layout.simple_list_item_1, 
        new ArrayList<String>(arrMonths));
    

    【讨论】:

      【解决方案2】:

      好的,我在@Michael Krause 的帮助下解决了这个问题。多谢兄弟!我复制了另一个临时数组列表,并在 textchangedlistener 中使用。

      public class ItemList extends Activity {
      
          ListView lvItems;
          EditText tbSearch;
          //String[] arrMonth = {"January","February","March","April","May","June","July","August","September","October","November","December"};
          ArrayList<String> arrMonth = new ArrayList<>();
          ArrayList<String> temp = new ArrayList<>();
          ArrayAdapter<String> aaItems;
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_item_list);
              lvItems = (ListView) findViewById(R.id.lvItems);
              tbSearch = (EditText) findViewById(R.id.tbSearch);
              PopulateArrayList();
              temp.addAll(arrMonth);
      
              aaItems = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrMonth);
              lvItems.setAdapter(aaItems);
      
              tbSearch.addTextChangedListener(new TextWatcher() {
      
                  @Override
                  public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      
                  }
      
                  @Override
                  public void onTextChanged(CharSequence s, int start, int before, int count) {
                      String txt = tbSearch.getText().toString().toLowerCase();
                      aaItems.clear();
                      if (s.length() == 0) {
                          aaItems.addAll(temp);
                      } else {
                          for (String item : temp) {
                              if (item.toLowerCase().contains(txt)) {
                                  aaItems.add(item);
                              }
                          }
                      }
                  }
      
                  @Override
                  public void afterTextChanged(Editable s) {
      
                  }
              });
      
          }
      
          public void btnAdd_Click(View v)
          {
              aaItems.add("Patrick");
          }
      
          public void PopulateArrayList()
          {
              arrMonth.add("January");
              arrMonth.add("February");
              arrMonth.add("March");
              arrMonth.add("April");
              arrMonth.add("May");
              arrMonth.add("June");
              arrMonth.add("July");
              arrMonth.add("August");
              arrMonth.add("September");
              arrMonth.add("October");
              arrMonth.add("November");
              arrMonth.add("December");
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-07-13
        • 1970-01-01
        • 2018-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-29
        相关资源
        最近更新 更多