搜索结果列表未设置为recyclerview

时间:2017-06-21 06:32:51

标签: android android-recyclerview realm adapter searchview

  

我使用realmData实现搜索功能我已经打印了搜索字符串但搜索到的字符串没有设置为recyclerview。

     

realm serchfilter返回完整结果并打印出来但返回mFilteredList未设置为recyclerview

package "";
public class FollowAdapter extends RecyclerView.Adapter<FollowAdapter.Holder> implements Filterable {
    private Context context;
    ActionListener actionListener;
    InviteListener inviteListener;
    private String TAG = getClass().getName();

    List<ContactClass> realmList;
    private List<ContactClass> mFilteredList;


    public FollowAdapter(Context context, List<ContactClass> realmList) {
        this.context = context;
        this.realmList = realmList;
        mFilteredList = realmList;

    }

    public InviteListener getInviteListener() {
        return inviteListener;
    }

    public void setInviteListener(InviteListener inviteListener) {
        this.inviteListener = inviteListener;
    }

    public ActionListener getActionListener() {
        return actionListener;
    }

    public void setActionListener(ActionListener actionListener) {
        this.actionListener = actionListener;
    }


    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.follow_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final Holder holder, final int position) {
        final ContactClass contactClass = realmList.get(position);
        holder.contactName.setText(contactClass.getName());
         Drawable drawable = ResourcesCompat.getDrawable(holder.itemView.getContext().getResources(), R.drawable.camera, null);
         Glide.with(holder.itemView.getContext()).load(contactClass.getAvatar()).into(holder.contact_pic).onLoadFailed(drawable);
          holder.contactNumber.setText(contactClass.getNumber());
        }


    }


    @Override
    public int getItemCount() {
        return realmList.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {

                String charString = charSequence.toString();

                if (charString.isEmpty()) {
                    mFilteredList = realmList;
                } else {
                    List<ContactClass> filteredList = new ArrayList<>();
                    Realm realm = Realm.getDefaultInstance();

                    RealmQuery<ContactClass> query = realm.where(ContactClass.class).contains("name", charString, Case.INSENSITIVE);
                    RealmResults<ContactClass> realmResults = query.findAll();

                    for (ContactClass c : realmResults) {
                        filteredList.add(c);
                        Log.e("filter", c.getName() + "  " + c.getNumber() + "  " + c.getId());
                    }

                    mFilteredList = filteredList;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = mFilteredList;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                if (mFilteredList != null) {
                    Log.e("sized", mFilteredList.size() + "");
                }
                mFilteredList = (List<ContactClass>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }

    public class Holder extends RecyclerView.ViewHolder {
        ImageView contact_pic;
        TextView contactName, contactNumber;
        AppCompatButton follow;

        public Holder(View itemView) {
            super(itemView);
            contact_pic = itemView.findViewById(R.id.contact_pic);
            contactName = itemView.findViewById(R.id.contact_name);
            contactNumber = itemView.findViewById(R.id.contact_number);
            follow = itemView.findViewById(R.id.btn_follow);
            contactName.setSelected(true);
            contactNumber.setSelected(true);
        }
    }}

搜索查询的代码

 @Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    followAdapter.getFilter().filter(newText);
    return false;
}

0 个答案:

没有答案