android - DisplayListCanvas 在未绑定(bind)的 RenderNode 上启动(没有 mOwningView)

标签 android android-arrayadapter autocompletetextview

我正在尝试使用我的自定义 ArrayAdapter 填充 AutoCompleteTextView。我认为添加显示值效果很好。唯一的问题是没有显示下拉菜单。有谁知道如何让这个下拉菜单可见? 每次我应该看到下拉菜单时,我都会看到日志消息:

DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)

我的适配器代码:

public class UserSearchAdapter extends ArrayAdapter<Profile> {

    Context context;
    ArrayList profiles;


    public UserSearchAdapter(Context context, int resource, ArrayList<Profile> objects) {
        super(context, resource, objects);
        this.context = context;
        this.profiles = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(this.getContext())
                    .inflate(R.layout.single_user_item, parent, false);
        }

        Profile profile = (Profile) profiles.get(position);

        TextView text = (TextView) convertView.findViewById(R.id.single_user_name_text);
        text.setText(profile.getDisplayName());

        return convertView;
    }

}

我的 MainActivity 代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_member_supervisor);
    activity = MemberSupervisorActivity.this;

    Firebase.setAndroidContext(this);

    autoCompleteUser = (AutoCompleteTextView) findViewById(R.id.auto_members);

    profiles = new ArrayList<>();

    members = new UserSearchAdapter(activity, R.layout.single_user_item, profiles);

    autoCompleteUser.setAdapter(members);
    autoCompleteUser.setThreshold(1);

    autoCompleteUser.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) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            final Query auto = new Firebase(Constants.FIREBASE_USER_PROFILE).orderByChild("displayName").startAt(autoCompleteUser.getText().toString()).endAt(autoCompleteUser.getText().toString() + "~");
            auto.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    profiles.clear();
                    Log.d("entered", autoCompleteUser.getText().toString());
                    for (DataSnapshot data : dataSnapshot.getChildren()) {
                        Profile profile = data.getValue(Profile.class);
                        profiles.add(profile);
                        Log.d("results", profile.getDisplayName());
                    }
                    members.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });
        }
    });

}

最佳答案

修改您的适配器,其中包含过滤逻辑,如下所示。

public class UserSearchAdapter extends ArrayAdapter<Profile> {

    Context context;
    ArrayList profiles;
    ArrayList<Profile> suggestions, tempItems;

    public UserSearchAdapter(Context context, int resource, ArrayList<Profile> objects) {
        super(context, resource, objects);
        this.context = context;
        this.profiles = objects;
        this.suggestions=new ArrayList();
        tempItems = new ArrayList(Profile);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(this.getContext())
                    .inflate(R.layout.single_user_item, parent, false);
        }

        Profile profile = (Profile) profiles.get(position);

        TextView text = (TextView) convertView.findViewById(R.id.single_user_name_text);
        text.setText(profile.getDisplayName());

        return convertView;
    }

    @Override
    public Filter getFilter() {
        return nameFilter;
    }

    /**
     * Custom Filter implementation for custom suggestions we provide.
     */
    Filter nameFilter = new Filter() {
        @Override
        public CharSequence convertResultToString(Object resultValue) {
            String str = ((Profile) resultValue).getName();
            return str;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if (constraint != null) {
                suggestions.clear();
                for (Profile profile : tempItems) {
                    if (profile.getDisplayName().toLowerCase().contains(constraint.toString().toLowerCase())) {
                        suggestions.add(profile);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            List<Profile> filterList = (ArrayList<Profile>) results.values;
            if (results != null && results.count > 0) {
                clear();
                for (Profile profile : filterList) {
                    add(profile);
                    notifyDataSetChanged();
                }
            }
        }
    };
}

注意:import android.widget.Filter; 希望这有帮助!!!

关于android - DisplayListCanvas 在未绑定(bind)的 RenderNode 上启动(没有 mOwningView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35900615/

相关文章:

java - 当可以使用旧变量时,为什么要创建并使用新变量呢?

Android 在适配器上重叠文本

android - AutoCompleteTextView 只允许建议的选项

安卓微调器 : how to match parent and append the dropdown to the bottom

android - 无法过滤 URL NDEF Android Intent

java - 为什么 ArrayAdapter 只从数组的每个成员中检索一个对象,而不是整个对象?

android - 自定义 Android AutoCompleteTextView

android - 无法设置开机重复闹钟

android - 如何禁止 USB 权限对话框?

android - 关于从 ListView 中检索 View 的困惑