您的位置:首页 > 其它

listview的过滤

2015-07-14 11:58 176 查看
这个也是我自己通过网上找资料,自己写的一个Demos。

内容简单,直接代码献上;

public class MainActivity extends Activity {

     

     List<People> people = new ArrayList<People>() ;

     EditText editinput;

     ListView listview;

     Adapter adapter;

     

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         setContentView(R.layout.activity_main);

         editinput = (EditText)findViewById(R.id.edit);

        listview = (ListView)findViewById(R.id.ydlist);

         initdata();

        adapter = new Adapter( people);

         listview.setAdapter(adapter);

         editinput.addTextChangedListener(new watcher());

     }

     

      void initdata(){

         

         people.add(new People("张三","1374456"));

         people.add(new People("张三小子", "12444455"));

         people.add(new People("李一", "1345555"));

         people.add(new People("王一", "1355555"));

         people.add(new People("王二", "1365555"));

         people.add(new People("李三", "13565555"));

         people.add(new People("李一", "123555"));

      }

     

     class watcher implements TextWatcher{

 

 

         @Override

         public void beforeTextChanged(CharSequence s, int start, int count,

                 int after) {

             // TODO Auto-generated method stub    

             

         }

 

         @Override

         public void onTextChanged(CharSequence s, int start, int before,

                 int count) {

             // TODO Auto-generated method stub

             String aa = s.toString();

             Pattern p = Pattern.compile(aa);

             List<People> we = new ArrayList<People>();        

             for(int i=0;i<people.size();i++){

                 People pp = people.get(i);

             Matcher matcher = p.matcher(pp.getName()+pp.getPhome());

             if(matcher.find()){

                 we.add(pp);

             }

             }

             adapter = new Adapter( we);

            listview.setAdapter(adapter);

        }

@Override

public void afterTextChanged(Editable s) {

// TODO Auto-generated method stub

}

         

     }

     public class Adapter extends BaseAdapter  {

           private List<People> people = new ArrayList<People>();

       Adapter(List<People> people) {

               // TODO Auto-generated constructor stub

               this.people = people;

           }

       

           @Override

           public int getCount() {

               // TODO Auto-generated method stub

               return people.size();

           }

       

           @Override

           public Object getItem(int position) {

               // TODO Auto-generated method stub

               return people.get(position);

           }

       

           @Override

           public long getItemId(int position) {

               // TODO Auto-generated method stub

               return position;

           }

       

           @Override

           public View getView(int position, View convertView, ViewGroup parent) {

               // TODO Auto-generated method stub

               People p = people.get(position);

               if(convertView==null){

                   convertView = getLayoutInflater().inflate(R.layout.yd_item, null);

               }

               TextView tv1 = (TextView) convertView.findViewById(R.id.ydtext1);

               TextView tv2 = (TextView) convertView.findViewById(R.id.ydtext2);

               tv1.setText(p.getName());

               tv2.setText(p.getPhome());

               return convertView;

           }

       

           

       

       }

     public class People {

          private String Name ;

           private String Phome;

           

          

           

           public String getName() {

              return Name;

          }

      

      

      

          public void setName(String name) {

              Name = name;

          }

      

      

      

          public String getPhome() {

              return Phome;

          }

      

      

      

          public void setPhome(String phome) {

              Phome = phome;

          }

      

      

      

          public People(String name,String phone){

              super();

              this.Name = name;

              this.Phome = phone;    

          }

      }

 }

xml的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

    <EditText

        android:id="@+id/edit"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:layout_marginLeft="82dp"

        android:ems="10" >

        <requestFocus />

    </EditText>

    <ListView

        android:id="@+id/ydlist"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_alignParentLeft="true"

        android:layout_below="@+id/edit" >

    </ListView>

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  listview 控件 class