在Android的过滤器是采取了错误的位置 [英] Filter in android is taking the wrong position

查看:221
本文介绍了在Android的过滤器是采取了错误的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对列表进行过滤器使用适配器的EditText进行排序。

过滤器是处理好但列表视图点击的位置始终不变。

即。 ListView控件是越来越过滤器,但其采取的列表视图中的相同位置选择项目后。

FirstScreen看起来是这样的,当选择了阿尔巴尼亚阿尔巴尼亚时显示正常,但在排序behrain后得到选择,并显示相同的阿尔巴尼亚因为poition是0在列表视图中如何解决这个问题,

​​

一些code适配器是:

  @覆盖
公共过滤用getFilter(){

    过滤器过滤器=新的过滤器(){

        @燮pressWarnings(未登记)
        @覆盖
        保护无效publishResults(CharSequence的约束,
                FilterResults结果){

            country_inf =(ArrayList的< CustomCountry codesPojo>)results.values​​;
            notifyDataSetChanged();
        }

        @覆盖
        保护FilterResults performFiltering(CharSequence的约束){

            FilterResults结果=新FilterResults();
            ArrayList的< CustomCountry codesPojo> FilteredArrayNames =新的ArrayList< CustomCountry codesPojo>();

            如果(mOriginalNames == NULL){
                mOriginalNames =新的ArrayList< CustomCountry codesPojo>(
                        country_inf);
            }
            如果(约束== NULL || constraint.length()== 0){
                results.count = mOriginalNames.size();
                results.values​​ = mOriginalNames;
            } 其他 {
                约束= constraint.toString()与toLowerCase()。
                的for(int i = 0; I< mOriginalNames.size();我++){
                    CustomCountry codesPojo dataNames = mOriginalNames
                            获得(ⅰ);
                    如果(dataNames.countryName.toString()。与toLowerCase()
                            。载(constraint.toString())){
                        FilteredArrayNames.add(dataNames);
                    }
                }

                results.count = FilteredArrayNames.size();
                //的System.out.println(results.count);

                results.values​​ = FilteredArrayNames;
                // Log.e(价值观,results.values​​.toString());
            }

            返回结果;
        }
    };

    返回过滤器;
}
 

我的问题是,在列表项后点击某些搜索其采取从第一位置

ListView中点击code是

  lv_list.setOnItemClickListener(新OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,
                长ARG3){
            意图I =新意图();
            i.putExtra(flagId,flagId);
            i.putExtra(名,allRecords.get(ARG2).c​​ountryName);
            的setResult(RESULT_OK中,(i).setAction(正常));
            完();

        }
    });
 

解决方案

这是一个常见的​​问题使用了列表过滤器时。

例如:
问题是:

  • 过滤之前,
    ArrayList的 - > [AA,BB,AC]
    列表 - >
    AA
    BB
    AC

  • 在过滤,
    ArrayList的 - > [AA,BB,AC]
    列表 - >
    AA
    AC

所以,使用 ARG2 位置),过滤后,如果你点击'交流', ArrayList的时候获得(ARG2)将返回BB。

解决方法:

使用 ARG1 查看)代替。这是当前单列项目视图,你点击了。


我认为你正在使用的ImageView和一个TextView自定义列表行。那么对于国家的名字,我会code为,

 ((TextView中)arg1.findViewById(R.id.textview1))。getText.toString()
 

I am trying to sort the list by EditText using filter in adapter.

Filter is processing well but the position of listview click is always constant.

i.e. ListView is getting filter but after selection of item its taking the same position in the list view.

FirstScreen looks like this and when when the albania is selected albania is displayed properly but when after sorting behrain get selected and same albania is displayed since poition is 0 in the list view how to rectify this issue

Some of the code in adapter is :

@Override
public Filter getFilter() {

    Filter filter = new Filter() {

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {

            country_inf = (ArrayList<CustomCountryCodesPojo>) results.values;
            notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();
            ArrayList<CustomCountryCodesPojo> FilteredArrayNames = new ArrayList<CustomCountryCodesPojo>();

            if (mOriginalNames == null) {
                mOriginalNames = new ArrayList<CustomCountryCodesPojo>(
                        country_inf);
            }
            if (constraint == null || constraint.length() == 0) {
                results.count = mOriginalNames.size();
                results.values = mOriginalNames;
            } else {
                constraint = constraint.toString().toLowerCase();
                for (int i = 0; i < mOriginalNames.size(); i++) {
                    CustomCountryCodesPojo dataNames = mOriginalNames
                            .get(i);
                    if (dataNames.countryName.toString().toLowerCase()
                            .contains(constraint.toString())) {
                        FilteredArrayNames.add(dataNames);
                    }
                }

                results.count = FilteredArrayNames.size();
                // System.out.println(results.count);

                results.values = FilteredArrayNames;
                // Log.e("VALUES", results.values.toString());
            }

            return results;
        }
    };

    return filter;
}

My problem is that in list item click after some search its taking from the first position

ListView click code is

lv_list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Intent i = new Intent();
            i.putExtra("flagId", flagId);
            i.putExtra("name", allRecords.get(arg2).countryName);
            setResult(RESULT_OK, (i).setAction("ok"));
            finish();

        }
    });

解决方案

This is a common problem when using a filter for the list.

Eg:
Problem is:

  • Before filtering,
    ArrayList-->[AA,BB,AC]
    List-->
    AA
    BB
    AC

  • After filtering,
    ArrayList-->[AA,BB,AC]
    List-->
    AA
    AC

So, when usingarg2(position), after filtering, if you click on 'AC', arraylist.get(arg2) will return 'BB'.

Solution:

Use arg1(View) instead. It is the current single row item View, which you have clicked.


I think you are using an ImageView and a TextView for custom list row. Then for country name, I'll code as,

((TextView)arg1.findViewById(R.id.textview1)).getText.toString()

这篇关于在Android的过滤器是采取了错误的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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