在ArrayAdapter android listview中实现一个getFilter()

时间:2014-10-06 10:33:23

标签: android android-listview

在我的列表视图中有四个文本视图和图像。

我想在 txtcompany 文字视图

上设置过滤器

我正在尝试实现一个getFilter(),但它提供了错误的结果

请帮助我如何在listview中实现getFilter()

列表视图ArrayAdapter源代码
     class Data extends ArrayAdapter          {

            Button imageView;
            TextView txtcompany;
            TextView txtDesc;
            TextView txtPosition;
            TextView txtState;
            TextView txtCity;
            String[] companyarray;
            String[] positonarray;
            String[] cityarray;
            String[] statearray;
            String[] Descarray;
            String[] contry;
            String[] pass;
            ArrayList<String> receiceValueOfAdapter=new ArrayList<String>(6);
            ArrayList<String> time=new ArrayList<String>(6);
         Context context;
         Data(Context c, String[] company, String[] position, String[] city, String[] state,ArrayList<String> receiveValue, String[] Desc,ArrayList<String> time1,
                 String[] pass,String[] contry)
         {
             super(c,R.layout.list_item,R.id.txt_company,company);
             this.context=c;
             this.pass=pass;
             this.companyarray=company;
             this.positonarray=position;
             this.cityarray=city;
             this.statearray=state;
             this.receiceValueOfAdapter=receiveValue;
             this.Descarray=Desc;
             this.time=time1;
             this.contry=contry;
         }
         public View getView(int position, View convertView, ViewGroup parent) {
             final String description;
             String upperString="";
             LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
             View row;
             row=mInflater.inflate(R.layout.list_item, parent,false);
             txtcompany = (TextView) row.findViewById(R.id.txt_company);
             imageView = (Button) row.findViewById(R.id.img);
             //imageView.setVisibility(View.INVISIBLE);
             txtPosition= (TextView) row.findViewById(R.id.txt_position);
             txtCity= (TextView) row.findViewById(R.id.txt_city);
             txtState= (TextView) row.findViewById(R.id.txt_state);

             if(companyarray[position].toString().length()>0)
            upperString = companyarray[position].substring(0,1).toUpperCase() + companyarray[position].substring(1);
             txtcompany.setText(" "+upperString);
             txtPosition.setText("  "+positonarray[position]);
             Log.d("City",statearray[position]+"City "+cityarray[position]);
             if(cityarray[position].toString().length()==0)
             {
                 txtCity.setText(statearray[position]);

             }
            if(statearray[position].toString().trim().length()==0)
             {
                 txtCity.setText("  "+cityarray[position]);
             }
            if(statearray[position].toString().trim().length()<2 && cityarray[position].toString().trim().length()<2)
             {
                 txtCity.setText("  "+contry[position]); 

             }

            if(statearray[position].toString().trim().length()>=2 && cityarray[position].toString().trim().length()>=2)
             {

                 txtCity.setText(" "+cityarray[position]+", "+statearray[position]);
             }

             txtState.setText(""+time.get(position));
             imageView.setTag(receiceValueOfAdapter.get(position));

             description= Descarray[position];
             final int l=position;
             imageView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(final View v) {
                    // Toast.makeText(contect, ""+ txtPosition.getText().toString(), Toast.LENGTH_LONG).show();


                        try{
                            String strurl1=pass[l].trim();
                            String strurl= strurl1.trim();
                            if (!strurl.trim().startsWith("https://") && !strurl.trim().startsWith("http://")){
                                strurl = "http://"+ strurl;
                            }
                        Intent ii = new Intent(Intent.ACTION_VIEW);
                        ii.setData(null);
                        ii.setData(Uri.parse(strurl));
                        startActivity(ii);

                        }
                        catch(Exception e)
                        {

                        }


                    }
                });
             return row;
         }
     }

EditText更改侦听器java代码

 lstsearch.addTextChangedListener(new TextWatcher()
                 {
                     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
                     {

                     }

                     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
                     {
                         Main_listview.this.datadap.getFilter().filter(charSequence);   
                     }

                     public void afterTextChanged(Editable editable)
                     {


                     }
                 });   

1 个答案:

答案 0 :(得分:0)

您可以像这样创建自定义方法。

public void filter(String charText,int flag) 

{
   if(flag==0){
charText = charText.toLowerCase(Locale.getDefault());


if (charText.length() == 0) 

{

      ListofDetails.addAll(arraylist);

    } 
    else 
    {
        for (ListofDetails wp : arraylist) 
        {
            if (wp.getCountry().toLowerCase(Locale.getDefault()).contains(charText)) 
            {
                NewList.add(wp);
            }
        }
    }


 elseif

{

 ///do some code

}

}