如何通过 EditText 过滤 ListView [英] How to Filter ListView through EditText

查看:25
本文介绍了如何通过 EditText 过滤 ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个类似安卓应用程序的电子书,我想过滤书名,但每次你在编辑文本中输入一个词或句子时,它都会搜索书的内容......有人可以帮我解决这个问题吗...

Im making an eBook like application for android and i want to filter the title of the book but everytime that you put a word or sentence in the edittext it will search the content of the books... can someone help me with this...

推荐答案

试试这个,每当你在 edittext 中输入文本时,列表将显示过滤结果,因为我已经在图像中显示了内容

try this one,whenever u enter text in the edittext ,the list will show filtered result as i have shown the stuff in images

初始:

过滤:


这是main.xml


this is main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText 
    android:id="@+id/etSearchbox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<ListView 
    android:id="@+id/lvFirst"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    ></ListView>    

</LinearLayout>

这是 FilterListActivity.java

this is FilterListActivity.java

package com.filterlist;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class FilterListActivity extends Activity{

EditText etSearchbox;
ListView lvFirst;
ArrayAdapter<String> adapter1;
String[] data = {"mehul joisar","amit mishra","amitabh","Aamir khan","jesica","katrina"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

etSearchbox=(EditText)findViewById(R.id.etSearchbox);
lvFirst=(ListView)findViewById(R.id.lvFirst);
lvFirst.setTextFilterEnabled(true);

adapter1 = new ArrayAdapter<String>(FilterListActivity.this, android.R.layout.simple_list_item_1, data);   
lvFirst.setAdapter(adapter1);

etSearchbox.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        FilterListActivity.this.adapter1.getFilter().filter(arg0);
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

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

    }
});




}
}

这篇关于如何通过 EditText 过滤 ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆