从适配器访问时的EditText被返回null [英] EditText is returning null when accessed from an adapter

查看:136
本文介绍了从适配器访问时的EditText被返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的系统使用的ListView 用getFilter 功能的适配器类。我想,这样当搜索是空的,的EditText文本颜色变成红色对其进行修改。我有以下的code不FC我的应用程序,但返回的字符串是空白的,而不是什么是在的EditText

我的部分适配器类:

  @燮pressWarnings(未登记)
        @覆盖
        保护无效publishResults(CharSequence的约束,FilterResults结果){
            数据=(ArrayList的< SetRows>)results.values​​;
            如果(data.isEmpty()){
                LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                查看MyView的= inflater.inflate(R.layout.main,NULL);
                的EditText myTextView =(EditText上)myView.findViewById(R.id.etSearch);
                myTextView.setTextColor(Color.RED);
                Toast.makeText(的getContext(),myTextView.getText()的toString(),2000年。).show();
            }
            notifyDataSetChanged();
            明确();
            的for(int i = 0,1 = data.size(); I<升;我++)
                添加(data.get(I));
            notifyDataSetInvalidated();
        }

我如何修改code,这样我可以实现的结果?


解决方案

 公共类YourActivity扩展活动
{
    私人的EditText EDITTEXT = NULL;
    //等等。    @覆盖
    保护无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main_layout);        EDITTEXT =(EditText上)findViewById(R.id.my_edit_text);
        //等等。
    }    私人过滤listFilter =新的过滤器(){
        @覆盖
        保护FilterResults performFiltering(CharSequence的约束)
        {
        }        @覆盖
        保护无效publishResults(CharSequence的约束,FilterResults结果)
        {
            ArrayList的< SetRows>数据=(ArrayList的< SetRows>)results.values​​;
            如果(data.isEmpty())YourActivity.this.editText.setTextColor(Color.RED);
            Toast.makeText(的getContext(),myTextView.getText()的toString(),2000年。).show();
        }
    };
}

I have an adapter class which uses the getFilter function within a ListView. I wanted to modify it so that when the search is empty, the EditText text color becomes red. I have the following code which doesn't FC my app but the return string is blank instead of what is in the EditText

My partial adapter class:

@SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            data = (ArrayList<SetRows>)results.values;
            if (data.isEmpty()) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View myView = inflater.inflate(R.layout.main, null);
                EditText myTextView = (EditText) myView.findViewById(R.id.etSearch);
                myTextView.setTextColor(Color.RED);
                Toast.makeText(getContext(), myTextView.getText().toString(), 2000).show();
            }
            notifyDataSetChanged();
            clear();
            for(int i = 0, l = data.size(); i < l; i++)
                add(data.get(i));
            notifyDataSetInvalidated();
        }

How do I modify the code so that I can achieve the result?

解决方案

public class YourActivity extends Activity
{
    private EditText editText = null;
    // etc.

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        editText = (EditText) findViewById(R.id.my_edit_text);
        // etc.
    }

    private Filter listFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint)
        {
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results)
        {
            ArrayList<SetRows> data = (ArrayList<SetRows>) results.values;
            if (data.isEmpty()) YourActivity.this.editText.setTextColor(Color.RED);
            Toast.makeText(getContext(), myTextView.getText().toString(), 2000).show();
        }
    };
}

这篇关于从适配器访问时的EditText被返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆