Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayAdapter addAll函数不';似乎没有被调用_Java_Android - Fatal编程技术网

Java ArrayAdapter addAll函数不';似乎没有被调用

Java ArrayAdapter addAll函数不';似乎没有被调用,java,android,Java,Android,我下面的代码用于列表视图的搜索过滤器。每次更改tbSearcheditText中的文本时,都必须更改listview中的项目。执行进入if语句(txt.length()==0),但它不添加我的数组 public void onTextChanged(CharSequence s, int start, int before, int count) { String txt = tbSearch.getText().toString().toLowerCase(); aaItems

我下面的代码用于列表视图的搜索过滤器。每次更改
tbSearch
editText中的文本时,都必须更改listview中的项目。执行进入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);
            }
        }
    }
}

传递给ArrayAdapter的列表用作适配器的支持ArrayList

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

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

aaItems = new ArrayAdapter(this, android.R.layout.simple_list_item_1, 
    new ArrayList<String>(arrMonths));
aaItems=newarrayadapter(这是android.R.layout.simple\u list\u item\u 1,
新ArrayList(arryList);

好的,我在@Michael Krause的帮助下解决了这个问题。谢谢兄弟!我复制了另一个数组列表temp,并在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");
    }
}
public类ItemList扩展活动{
列表视图项目;
编辑文本搜索;
//字符串[]arrmount={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};
ArrayList arrMonth=新的ArrayList();
ArrayList temp=新的ArrayList();
ArrayAdapter项目;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u item\u list);
lvItems=(ListView)findViewById(R.id.lvItems);
tbSearch=(EditText)findViewById(R.id.tbSearch);
PopulateArrayList();
温度加总(月);
aaItems=newarrayadapter(这个,android.R.layout.simple\u list\u item\u 1,arrMonth);
lvItems.setAdapter(aaItems);
tbSearch.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
字符串txt=tbSearch.getText().toString().toLowerCase();
aaItems.clear();
如果(s.length()==0){
aaItems.addAll(临时);
}否则{
用于(字符串项:temp){
if(item.toLowerCase().contains(txt)){
aa项目。添加(项目);
}
}
}
}
@凌驾
公共无效后文本已更改(可编辑){
}
});
}
公共无效btnAdd_单击(视图v)
{
aa项目。添加(“帕特里克”);
}
public void PopulateArrayList()
{
添加(“一月”);
添加(“二月”);
添加(“三月”);
添加(“四月”);
添加(“五月”);
添加(“六月”);
添加(“7月”);
添加(“8月份”);
添加(“9月”);
添加(“10月”);
添加(“11月”);
添加(“12月”);
}
}

您是否在适配器上安装了
notifyDataSetChanged
?请看下面的答案-notifyDataSetChanged对我不起作用。我的arraylist似乎被清除了,我不知道为什么。我只清除我的arrayadapter,而不清除我的arraylist。arrayadapter调用notifyDataSetChanged,在clear()和addAll()调用中都为您更改了,因此无需调用您自己。我尝试创建了一个备份arraylist,但它仍然被清除。因此,您创建了一个无法修改的最终对象。但是如果我的ArrayList来自数据库呢。这意味着我不能使用那样的固定列表。没关系。只是不要使用同一个ArrayList来保存所有可能的值,就像您传递给ArrayAdapter的值一样。如果您不做其他事情,只需创建一个新的ArrayList,在创建ArrayAdapter时将arrMonth列表作为参数传递给它,您就会明白我的意思。@PatrickDeVera:我已经修改了我的答案,以适应您的特定问题。你应该可以用我的替换你的ArrayAdapter生产线,很好。很酷,很高兴它对你有用,帕特里克。我有两个要求:如果我的回答对你有帮助,你能接受我的回答吗?第二,主持人可能会让你回答你自己的问题,而不是修改你原来的问题。仅供参考。:)@迈克尔克劳斯:完全可以回答你自己的问题。这里最主要的一点是看看它是否真的回答了这个问题。