android listview 过滤,listview的过滤

这个也是我自己通过网上找资料,自己写的一个Demos。

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

public class MainActivity extends Activity {

List people = new ArrayList() ;

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 we = new ArrayList();

for(int i=0;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 = new ArrayList();

Adapter(List 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的代码:

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

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity" >

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" >

android:id="@+id/ydlist"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_alignParentLeft="true"

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android ListView搜索框是一种在ListView中实现搜索功能的控件。它通常位于ListView的顶部,用户可以在搜索框中输入关键词,ListView会根据输入的关键词实时过滤显示符合条件的数据。 实现ListView搜索框的一种常见方法是使用TextWatcher监听搜索框内容的变化,在TextWatcher中实时更新ListView的显示。具体步骤如下: 1. 在布局文件中添加ListView和搜索框: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <SearchView android:id="@+id/search_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> ``` 2. 在Activity或Fragment中获取ListView和搜索框的引用,并设置适配器: ```java ListView listView = findViewById(R.id.list_view); SearchView searchView = findViewById(R.id.search_view); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data); listView.setAdapter(adapter); ``` 3. 添加TextWatcher监听搜索框的文本变化: ```java searchView.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { adapter.getFilter().filter(s); } @Override public void afterTextChanged(Editable s) { } }); ``` 这样,当用户在搜索框中输入文本时,ListView会根据输入的文本实时过滤显示符合条件的数据。adapter.getFilter().filter(s)会根据输入的文本s对数据源进行过滤操作。 总结一下,Android ListView搜索框可以通过TextWatcher来监听搜索框的文本变化,并通过适配器的过滤机制实时更新ListView的显示,从而实现搜索功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值