listview的過濾-創(chuàng)新互聯(lián)

這個也是我自己通過網(wǎng)上找資料,自己寫的一個Demos。

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比太倉網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式太倉網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋太倉地區(qū)。費(fèi)用合理售后完善,十年實體公司更值得信賴。

內(nèi)容簡單,直接代碼獻(xiàn)上;

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>

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

新聞標(biāo)題:listview的過濾-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://muchs.cn/article40/coeoho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、網(wǎng)頁設(shè)計公司、做網(wǎng)站標(biāo)簽優(yōu)化、網(wǎng)站制作自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營