Android列表视图搜索无法正常运行

时间:2015-05-15 07:16:56

标签: android search android-listview android-filter android-filterable

请在我的代码中帮助我,它不会搜索完美所有其他值保留在他们的位置,除了搜索字符串。

以下是可过滤类的代码

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.StringTokenizer;
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Interpolator.Result;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.Filter;
    import android.widget.Filterable;
    import android.widget.TextView;


    public class ListViewAdapter_claimlist extends BaseAdapter implements Filterable{

    Activity context;
    String claimid[];
    String ref[];
    String policy[];
    String natureofloss[];
    String registration[];
    String status[];

    private ItemFilter mFilter = new ItemFilter();

     private List<String>originalData = null;
     private List<String>filteredData = null;

     private List<String>claimidFilter = null;
     private List<String>refFilter = null;
     private List<String>nautreoflossfilter = null;
     private List<String>registrationFilter = null;
     private List<String>statusFilter = null;

     ArrayList<abc> alldata = new ArrayList<abc>();


    public ListViewAdapter_claimlist(Activity context, String[] claimid, String[] ref,String[] policy, String[] natureofloss,String[] registration, String[] status) {
        super();
        this.context = context;
        this.claimid = claimid;
        this.ref = ref;
        this.policy = policy;
        this.natureofloss = natureofloss;
        this.registration = registration;
        this.status = status;

        this.filteredData = new ArrayList<String>(Arrays.asList(this.policy));
        this.originalData = new ArrayList<String>(Arrays.asList(this.policy));

        this.claimidFilter = new ArrayList<String>(Arrays.asList(this.claimid));
        this.refFilter = new ArrayList<String>(Arrays.asList(this.ref));
        this.nautreoflossfilter = new ArrayList<String>(Arrays.asList(this.natureofloss));
        this.registrationFilter = new ArrayList<String>(Arrays.asList(this.registration));
        this.statusFilter = new ArrayList<String>(Arrays.asList(this.status));

        for(int i = 0; i<this.claimid.length; i++)
        {
            abc a = new abc();
            a.setClaimid(claimid[i]);
            a.setRef(ref[i]);
            a.setPolicy(policy[i]);
            a.setNatureofLoss(natureofloss[i]);
            a.setRegistration(registration[i]);
            a.setStatus(status[i]);
            alldata.add(a);
        }

    }

    public int getCount() {
        return filteredData.size();
    }

    public Object getItem(int position) {
        return filteredData.get(position);
    }

    public long getItemId(int position) {
        return 0;
    }

    private class ViewHolder {
        TextView cliamid;
        TextView ref;
        TextView policy;
        TextView natureofloss;
        TextView registration;
        TextView status;
    }

    public View getView(int position, View convertView, final ViewGroup parent)
    {
        final ViewHolder holder;
        LayoutInflater inflater =  context.getLayoutInflater();
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.rowitem_cliamlist, null);
            holder = new ViewHolder();
            holder.cliamid = (TextView) convertView.findViewById(R.id.tv_claims);
            holder.ref = (TextView) convertView.findViewById(R.id.tv_referencepolicy);
            holder.policy = (TextView) convertView.findViewById(R.id.tv_policyNo);
            holder.natureofloss = (TextView) convertView.findViewById(R.id.tv_natureoflos);
            holder.registration = (TextView) convertView.findViewById(R.id.tv_registration);
            holder.status = (TextView) convertView.findViewById(R.id.tv_claimstatus);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        abc aa = alldata.get(position);
        holder.cliamid.setText(aa.getClaimid());
        holder.ref.setText(aa.getRef());
        holder.policy.setText(filteredData.get(position));
        holder.natureofloss.setText(aa.getNatureofloss());
        holder.registration.setText(aa.getRegistration());
        holder.status.setText(aa.getStatus());

    return convertView;

    }

    //--------------------------------------------------------------------------------
         public Filter getFilter() {
                return mFilter;
            }

            private class ItemFilter extends Filter {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {

                    String filterString = constraint.toString().toLowerCase();

                    FilterResults results = new FilterResults();

                    final List<String> list = originalData;

                    int count = list.size();
                    final ArrayList<String> nlist = new ArrayList<String>(count);

                    String filterableString ;

                    for (int i = 0; i < count; i++) {
                        filterableString = list.get(i);

                        if (filterableString.toLowerCase().contains(filterString)) {
                            nlist.add(filterableString);


                        }
                    }

                    results.values = nlist;
                    results.count = nlist.size();


                    return results;
                }

                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    filteredData = (ArrayList<String>) results.values;

                    notifyDataSetChanged();
                }

            }

            public class abc
            {
                String claimid;
                String ref;
                String policy;
                String natureofloss;
                String registration;
                String status;

                 public void setClaimid(String claimid)
                 { 
                     this.claimid = claimid;
                 }
                 public void setRef(String ref)
                 {
                     this.ref = ref;
                 }
                 public void setPolicy(String policy)
                 {
                     this.policy = policy;
                 }
                 public void setNatureofLoss(String natureofloss)
                 {
                     this.natureofloss = natureofloss;
                 }
                 public void setRegistration(String registration)
                 {
                     this.registration = registration; 
                 }
                 public void setStatus(String status)
                 {
                     this.status = status; 
                 }

                 public String getClaimid()
                 {
                     String claimid = this.claimid;
                     return claimid;
                 }
                 public String getRef()
                 {

                     String ref = this.ref;
                     return ref;
                 }
                 public String getPolicy()
                 {

                     String Policy = this.policy;
                     return Policy;
                 }
                 public String getNatureofloss()
                 {

                     String Natureofloss = this.natureofloss;
                     return Natureofloss;
                 }
                 public String getRegistration()
                 {

                     String Registration = this.registration;
                     return Registration;
                 }
                 public String getStatus()
                 {

                     String Status = this.status;
                     return Status;
                 }

            }

    }

编辑文本事件处理程序

searchText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                ClaimList.this.lviewAdapter.getFilter().filter(s.toString());
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

搜索前

enter image description here

警察搜索时没有(代码中的政策#)

enter image description here

提前感谢:)

1 个答案:

答案 0 :(得分:0)

您可以选择其他技巧,在编辑文本上设置textWatcher,输入搜索文本。

        searchText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = searchText.getText().toString()
                        .toLowerCase(Locale.getDefault());
                lviewAdapter.filter(text);

            }
        });

在Adapter类中放置一个过滤器方法

    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        temp.clear();
        if (charText.length() == 0) {
            temp.addAll(list_original);
        } else {
            for (Abc abc: list_original) {
                 // if you want to search via policy
                if (abc.getPolicy().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    temp.add(abc);
                }
            }
        }
        notifyDataSetChanged();
    }

创建两个Abc类型的arraylists,一个是temp,一个是原始的,总是将数据从临时列表中的getview方法传递给listview,这样你就可以对temp进行更改,而不是原来的。在上面的例子中,临时列表首先清除并运行循环以检查原始列表中的搜索文本是否匹配然后将该对象添加到临时列表,并调用notifyDataSetChanged .. Voila