Searchable list view through Edit text

2019-06-13 07:53发布

hello everyone I want develop application which have many item in array list and one Edit text for using search item fast in list view I show u image like that:

enter image description here

so how it possible in page

7条回答
相关推荐>>
2楼-- · 2019-06-13 08:30

Try this Simple Code: XML:

<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"
    android:orientation="vertical"
    tools:context=".SOF_AutoList" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Name" >

            <requestFocus />
        </EditText>
    </LinearLayout>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

Java Activity Class:

public class SOF_AutoList extends Activity
{
    private ListView listview;
    private EditText edttxt;
    private ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sof__auto_list);

        ArrayList<String> data = new ArrayList<String>();
        for(int i=0;i<10;i++)
        {
            if(i==0 || i==1 || i==3)
            {
                data.add("Apple "+(i+1));
            }
            else if(i==4 || i==5)
            {
                data.add("Banana "+(i+1));
            }
            else
            {
                data.add("CArrot "+(i+1));
            }
        }

        listview = (ListView)findViewById(R.id.listView1);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1,data);
        listview.setAdapter(adapter);

        edttxt = (EditText)findViewById(R.id.editText1);
        edttxt.addTextChangedListener(new TextWatcher()
        {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                adapter.getFilter().filter(s);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

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

            }
        });
    }


}
查看更多
登录 后发表回答