具有搜索功能示例的Android简单ListView

Here you will get android simple listview with search functionality example.

在这里,您将获得带有搜索功能示例的android简单列表视图。

Adding search functionality in listview helps users to find information in easy way. When user writes something in the textbox, the items in the list is filtered and an updated list of items is displayed.

在列表视图中添加搜索功能可帮助用户轻松找到信息。 当用户在文本框中输入内容时,列表中的项目将被过滤并显示更新的项目列表。

Below example shows you how to implement this.

以下示例显示了如何实现此目的。

具有搜索功能示例的Android简单ListView (Android Simple ListView with Search Functionality Example)

First of all create a new project with package name thecrazyprogrammer.androidexample. Now add following code in respective files.

首先,创建一个名为thecrazyprogrammer.androidexample的新项目。 现在,在相应文件中添加以下代码。

activity_main.xml (activity_main.xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<LinearLayout 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"
    android:orientation="vertical"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textBox"/>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"/>
</LinearLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<LinearLayout 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"
    android : orientation = "vertical"
    android : layout_marginLeft = "15dp"
    android : layout_marginRight = "15dp"
    android : layout_marginTop = "15dp"
    android : layout_marginBottom = "15dp" >
     <EditText
        android : layout_width = "match_parent"
        android : layout_height = "wrap_content"
        android : id = "@+id/textBox" />
     <ListView
        android : layout_width = "match_parent"
        android : layout_height = "wrap_content"
        android : id = "@+id/list" />
</LinearLayout>

list_item.xml (list_item.xml)

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        android:textSize="20dp"/>
</LinearLayout>
1
2
3
4
5
6
7
8
9
<? xml version = "1.0" encoding = "utf-8" ?>
<LinearLayout xmlns : android = "http://schemas.android.com/apk/res/android"
    android : layout_width = "match_parent" android : layout_height = "match_parent" >
     <TextView
        android : layout_width = "match_parent"
        android : layout_height = "wrap_content"
        android : id = "@+id/text"
        android : textSize = "20dp" />
</LinearLayout>

MainActivity.java (MainActivity.java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package thecrazyprogrammer.androidexample;
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;
import android.widget.TextView;
public class MainActivity extends Activity {
    EditText textBox;
    TextView text;
    ListView list;
    String country[]={"India","USA","China","Australia","Japan","England","Nepal","Canada","Sri-Lanka","Russia"};
    ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textBox=(EditText)findViewById(R.id.textBox);
        text=(TextView)findViewById(R.id.text);
        list=(ListView)findViewById(R.id.list);
        adapter=new ArrayAdapter(this,R.layout.list_item,R.id.text,country);
        list.setAdapter(adapter);
        textBox.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                adapter.getFilter().filter(s);
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package thecrazyprogrammer . androidexample ;
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 ;
import android . widget . TextView ;
public class MainActivity extends Activity {
     EditText textBox ;
     TextView text ;
     ListView list ;
     String country [ ] = { "India" , "USA" , "China" , "Australia" , "Japan" , "England" , "Nepal" , "Canada" , "Sri-Lanka" , "Russia" } ;
     ArrayAdapter adapter ;
     @Override
     protected void onCreate ( Bundle savedInstanceState ) {
         super . onCreate ( savedInstanceState ) ;
         setContentView ( R . layout . activity_main ) ;
         textBox = ( EditText ) findViewById ( R . id . textBox ) ;
         text = ( TextView ) findViewById ( R . id . text ) ;
         list = ( ListView ) findViewById ( R . id . list ) ;
         adapter = new ArrayAdapter ( this , R . layout . list_item , R . id . text , country ) ;
         list . setAdapter ( adapter ) ;
         textBox . addTextChangedListener ( new TextWatcher ( ) {
             @Override
             public void beforeTextChanged ( CharSequence s , int start , int count , int after ) {
                 adapter . getFilter ( ) . filter ( s ) ;
             }
             @Override
             public void onTextChanged ( CharSequence s , int start , int before , int count ) {
             }
             @Override
             public void afterTextChanged ( Editable s ) {
             }
         } ) ;
     }
}

Output

输出量

具有搜索功能示例的Android简单ListView
具有搜索功能示例的Android简单ListView

The above code is self-explanatory, if still you are facing difficulty to understand then feel free to comment below.

上面的代码是不言自明的,如果您仍然面临难以理解的问题,请在下面随意评论。

翻译自: https://www.thecrazyprogrammer.com/2016/01/android-simple-listview-with-search-functionality-example.html