猿问

使用getFilter函数时ListView为空白

我想实现getFilter()我的功能ListView,但每次我输入的东西EditText我ListView就会消失。


我的SetHelpRows档案:


public class SetHelpRows {

    String name;

    String id;


    public String getName () {

        return name;

    }


    public void setName (String name) {

        this.name = name;

    }


    public String getID () {

        return id;

    }


    public void setID (String id) {

        this.id = id;

    }


    public SetHelpRows(String name, String id) {

        super();

        this.name = name;

        this.id = id;

    }

}

我的部分MainActivity文件:


inputSearch = (EditText) findViewById(R.id.etSearch);

dataList = (ListView) findViewById(R.id.lvFiles);

for (int y=0; y<strNames.length;y++) {

    name = strNamesOfAllah[y];

    meaning = strMeaning[y];

    rowsArray.add(new SetRows(R.drawable.icon, name, meaning));

}

adapter = new SetRowsCustomAdapter(MainActivity.this, R.layout.customlist, rowsArray);

dataList.setAdapter(adapter);

dataList.setClickable(true);

inputSearch.addTextChangedListener(new TextWatcher() {

    @Override

    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

        // When user changed the Text

        //MainActivity.this.adapter.getFilter().filter(cs);

        //String text = inputSearch.getText().toString().toLowerCase();

        adapter.getFilter().filter(cs.toString().toLowerCase(Locale.US));

    }


    @Override

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

        //TODO Auto-generated method stub

    }


    @Override

    public void afterTextChanged(Editable arg0) {

        //TODO Auto-generated method stub                         

    }

});

在中,LogCat我确实看到以下内容:


12-12 12:15:57.077: I/Before Filter(720): Before Filter

12-12 12:15:57.106: I/prefix is !null or !0(720): prefix is !null or !0

12-12 12:15:57.216: I/publish result(720): publish result


慕田峪4524236
浏览 746回答 3
3回答

临摹微笑

您不需要实现自己的Filter,如果您使用,则ArrayAdapter可以重写toString方法SetHelpRows以返回名称。然后,您可以调用ArrayAdapter内置的Filter。这不是您问题的直接答案,而是建议更改您的实现。

qq_遁去的一_1

请在您的publishResults函数中尝试&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;protected void publishResults(CharSequence constraint, FilterResults results) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.i("publish result", "publish result");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;data = (ArrayList<SetRows>)results.values;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;notifyDataSetChanged();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
随时随地看视频慕课网APP

相关分类

Java
Android
我要回答