android– 如何过滤领域适配器中的数据?

android– 如何过滤领域适配器中的数据?,第1张

概述我在过滤适配器中的数据时遇到了问题.我正在创建searchview以通过过滤realm适配器中的数据来搜索数据.我的searchview实现代码是@OverridepublicvoidonCreateOptionsMenu(Menumenu,MenuInflaterinflater){inflater.inflate(R.menu.main_menu,menu);

我在过滤域适配器中的数据时遇到了问题.
我正在创建searchvIEw以通过过滤realm适配器中的数据来搜索数据.

我的searchvIEw实现代码是

  @OverrIDe    public voID onCreateOptionsMenu(Menu menu, MenuInflater inflater) {        inflater.inflate(R.menu.main_menu, menu);        final MenuItem item = menu.findItem(R.ID.action_search);        final SearchVIEw searchVIEw = (SearchVIEw) MenuItemCompat.getActionVIEw(item);        Log.e("Filter ","Filtering");        searchVIEw.setonqueryTextListener(this);        MenuItemCompat.setonActionExpandListener(item,                new MenuItemCompat.OnActionExpandListener() {                    @OverrIDe                    public boolean onMenuItemActionCollapse(MenuItem item) {// Do something when collapsed                        adapter.setFilter(realmPhonecallList);                        Log.e("Filter 1","Filtering");                        return true; // Return true to collapse action vIEw                    }                    @OverrIDe                    public boolean onMenuItemActionExpand(MenuItem item) {// Do something when expanded                        return true; // Return true to expand action vIEw                    }                });    }    @OverrIDe    public boolean onqueryTextsubmit(String newText) {        return true;    }    @OverrIDe    public boolean onqueryTextChange(String newText) {        final List<RealmPhoneCallLogs> filteredModelList = filter(realmPhonecallList, newText);        Log.e("Filter 2","Filtering" + newText);        adapter.setFilter(filteredModelList);        Log.e("Filter 3","Filtering" + filteredModelList);        return true;    }    private List<RealmPhoneCallLogs> filter(List<RealmPhoneCallLogs> models, String query) {        query = query.tolowerCase();        final List<RealmPhoneCallLogs> filteredModelList = new ArrayList<>();        for (RealmPhoneCallLogs model : models) {            final String text = model.getNumber().tolowerCase();            if (text.contains(query)) {                filteredModelList.add(model);            }        }        Log.e("Filter 4","Filtering" + filteredModelList);        return filteredModelList;    }

我的领域适配器实现是

private class MynamesAdapter extends RealmBaseAdapter<RealmPhoneCallLogs> {    List<RealmPhoneCallLogs> filter;    public MynamesAdapter(OrderedRealmCollection<RealmPhoneCallLogs> data) {        super(data);        this.filter = data;    }    public class VIEwHolder {        TextVIEw number;        TextVIEw callType;        TextVIEw startTime;        TextVIEw contactname;        TextVIEw callDuration;        TextVIEw fileSize;        ImageVIEw contactPhoto;        ImageVIEw callSymbol;    }    @OverrIDe    public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {        final RealmPhoneCallLogs realmPhoneCallLogs = getItem(position);        VIEwHolder vIEwHolder;        if (convertVIEw == null) {            vIEwHolder = new VIEwHolder();            LayoutInflater inflater = LayoutInflater.from(getContext());            convertVIEw = inflater.inflate(R.layout.calllog_layout, parent, false);            vIEwHolder.number = (TextVIEw) convertVIEw.findVIEwByID(R.ID.missednumber);            vIEwHolder.startTime = (TextVIEw) convertVIEw.findVIEwByID(R.ID.missedStartTime);            vIEwHolder.contactname = (TextVIEw) convertVIEw.findVIEwByID(R.ID.missedContactname);            vIEwHolder.callDuration = (TextVIEw) convertVIEw.findVIEwByID(R.ID.missedCallDuration);            vIEwHolder.fileSize = (TextVIEw) convertVIEw.findVIEwByID(R.ID.missedfileSize);            vIEwHolder.contactPhoto = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.missedImage);            vIEwHolder.callSymbol = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.callSymbol);            convertVIEw.setTag(vIEwHolder);        } else {            vIEwHolder = (VIEwHolder) convertVIEw.getTag();        }        if (adapterData != null) {            vIEwHolder.number.setText(realmPhoneCallLogs.getNumber());            vIEwHolder.contactname.setText(getContactname(realmPhoneCallLogs.getNumber()));            vIEwHolder.callDuration.setText(getTime(realmPhoneCallLogs.getCallDuration()));            vIEwHolder.startTime.setText(realmPhoneCallLogs.getStartTime());            vIEwHolder.contactPhoto.setimageBitmap(getContactsImage(realmPhoneCallLogs.getNumber()));            String path = new String(Environment.getExternalStorageDirectory() + "/NewCallLogs/" + realmPhoneCallLogs.getcallrecords());            file file = new file(path);            String size;            long filesize = file.length();            long fileSizeInKB = filesize / 1024;            long fileSizeInMB = fileSizeInKB / 1024;            if(fileSizeInKB >=1024){                 size= fileSizeInMB + " Mb";            }else {                 size = fileSizeInKB + " Kb";            }            if(!size.isEmpty()){                vIEwHolder.fileSize.setText(size);            }else{                vIEwHolder.fileSize.setText(0);            }            if (realmPhoneCallLogs.getCallType().equals("I")) {                vIEwHolder.callSymbol.setimageResource(R.mipmap.call_received);            } else if (realmPhoneCallLogs.getCallType().equals("O")) {                vIEwHolder.callSymbol.setimageResource(R.mipmap.call_made);            } else if (realmPhoneCallLogs.getCallStatus().equals("UA")) {                vIEwHolder.callSymbol.setimageResource(R.mipmap.call_missed);            }            return convertVIEw;        }        return convertVIEw;    }    public voID setFilter( List<RealmPhoneCallLogs> filtedData) {        filter = new ArrayList<>();        filter.addAll(filtedData);        notifyDataSetChanged();    }}

请帮我如何过滤领域的数据.

解决方法:

public voID setFilter( List<RealmPhoneCallLogs> filtedData) {    filter = new ArrayList<>(); // <-- WRONG    filter.addAll(filtedData); // <-- WRONGprivate List<RealmPhoneCallLogs> filter(List<RealmPhoneCallLogs> models, String query) {    query = query.tolowerCase();    final List<RealmPhoneCallLogs> filteredModelList = new ArrayList<>(); // <-- WRONG    for (RealmPhoneCallLogs model : models) { // <-- WRONG        final String text = model.getNumber().tolowerCase();        if (text.contains(query)) { // <-- WRONG: should be Realm query

相反,你需要这样做

// from https://stackoverflow.com/a/33818311/2413303searchVIEw.setonqueryTextListener(new SearchVIEw.OnqueryTextListener() {        @OverrIDe        public boolean onqueryTextsubmit(String query) {            adapter.getFilter().filter(query);            return false;        }        @OverrIDe        public boolean onqueryTextChange(String newText) {            if (searchVIEw.getquery().length() == 0) {                 adapter.getFilter().filter("");            }            return false;        }});

然后就像在the linked answer

private class MynamesAdapter        extends RealmBaseAdapter<RealmPhoneCallLogs>         implements Filterable {    public MynamesAdapter(OrderedRealmCollection<RealmPhoneCallLogs> data) {        super(data);    }    public class VIEwHolder {        TextVIEw number;        TextVIEw callType;        TextVIEw startTime;        TextVIEw contactname;        TextVIEw callDuration;        TextVIEw fileSize;        ImageVIEw contactPhoto;        ImageVIEw callSymbol;    }    @OverrIDe    public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {        final RealmPhoneCallLogs realmPhoneCallLogs = getItem(position);        VIEwHolder vIEwHolder;        if(convertVIEw == null) {            vIEwHolder = new VIEwHolder();            LayoutInflater inflater = LayoutInflater.from(getContext());            convertVIEw = inflater.inflate(R.layout.calllog_layout, parent, false);            vIEwHolder.number = (TextVIEw) convertVIEw.findVIEwByID(R.ID.missednumber);            vIEwHolder.startTime = (TextVIEw) convertVIEw.findVIEwByID(R.ID.missedStartTime);            vIEwHolder.contactname = (TextVIEw) convertVIEw.findVIEwByID(R.ID.missedContactname);            vIEwHolder.callDuration = (TextVIEw) convertVIEw.findVIEwByID(R.ID.missedCallDuration);            vIEwHolder.fileSize = (TextVIEw) convertVIEw.findVIEwByID(R.ID.missedfileSize);            vIEwHolder.contactPhoto = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.missedImage);            vIEwHolder.callSymbol = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.callSymbol);            convertVIEw.setTag(vIEwHolder);        } else {            vIEwHolder = (VIEwHolder) convertVIEw.getTag();        }        if(adapterData != null) {            vIEwHolder.number.setText(realmPhoneCallLogs.getNumber());            vIEwHolder.contactname.setText(getContactname(realmPhoneCallLogs.getNumber()));            vIEwHolder.callDuration.setText(getTime(realmPhoneCallLogs.getCallDuration()));            vIEwHolder.startTime.setText(realmPhoneCallLogs.getStartTime());            vIEwHolder.contactPhoto.setimageBitmap(getContactsImage(realmPhoneCallLogs.getNumber()));            String path = new String(Environment.getExternalStorageDirectory() + "/NewCallLogs/" + realmPhoneCallLogs                    .getcallrecords());            file file = new file(path);            String size;            long filesize = file.length();            long fileSizeInKB = filesize / 1024;            long fileSizeInMB = fileSizeInKB / 1024;            if(fileSizeInKB >= 1024) {                size = fileSizeInMB + " Mb";            } else {                size = fileSizeInKB + " Kb";            }            if(!size.isEmpty()) {                vIEwHolder.fileSize.setText(size);            } else {                vIEwHolder.fileSize.setText(0);            }            if("I".equals(realmPhoneCallLogs.getCallType())) {                vIEwHolder.callSymbol.setimageResource(R.mipmap.call_received);            } else if("O".equals(realmPhoneCallLogs.getCallType())) {                vIEwHolder.callSymbol.setimageResource(R.mipmap.call_made);            } else if("UA".equals(realmPhoneCallLogs.getCallStatus())) {                vIEwHolder.callSymbol.setimageResource(R.mipmap.call_missed);            }            return convertVIEw;        }        return convertVIEw;    }// filtering    public voID filterResults(String text) {        text = text == null ? null : text.tolowerCase().trim();        if(text == null || "".equals(text)) {            updateData(realm.where(RealmPhoneCallLogs.class).findAllSorted("ID");        } else {            updateData(realm.where(RealmPhoneCallLogs.class)                                .contains("number", text, Case.INSENSITIVE)                                .findAllSorted("ID"));         }    }    public Filter getFilter() {        return new MynamesFilter(this);    }    private class MynamesFilter            extends Filter {        private final MynamesAdapter adapter;        private MynamesFilter(MynamesAdapter adapter) {            super();            this.adapter = adapter;        }        @OverrIDe        protected FilterResults performFiltering(CharSequence constraint) {            return new FilterResults();        }        @OverrIDe        protected voID publishResults(CharSequence constraint, FilterResults results) {            adapter.filterResults(constraint.toString());        }    }}

要过滤Realm,您还可以查看official documentation on queries.

总结

以上是内存溢出为你收集整理的android – 如何过滤领域适配器中的数据?全部内容,希望文章能够帮你解决android – 如何过滤领域适配器中的数据?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1113719.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存