如何在片段类中的编辑文本上添加文本更改的侦听器

时间:2015-01-21 05:21:24

标签: android android-fragments

在我的自定义列表视图的片段类中。我还有一个编辑文本来过滤更改文本的列表记录。但有些我怎么也无法在fragmnet中实现它

here is my code :
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
{
    View view = inflater.inflate(R.layout.activity_main, null);

    llMain = (LinearLayout) view.findViewById(R.id.llMain); 
    llMain.setBackgroundColor(Color.GRAY);

    InputSearch=(EditText)view.findViewById(R.id.search);
    InputSearch.addTextChangedListener(new TextWatcher() 
    {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            adapter.getFilter().filter(s);// Error on this line: create method getFilter() in customadapter

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

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

        }
    });
}

2 个答案:

答案 0 :(得分:1)

将第adapter.getFilter().filter(s);行更改为MyActivity.this.adapter.getFilter().filter(s);

答案 1 :(得分:0)

您必须为自定义适配器实现Filterable并覆盖getFilter()方法。

public CustomAdapter extends BaseAdapter implements Filterable 
{
     //override all adapter methods

     @Override
    public Filter getFilter() {

        // your code here
        return null;
    }
}