自定义列表视图的搜索框

时间:2012-06-13 02:24:55

标签: android listview search

我构建了一个coustom列表视图,其中包含来自SQLite的数据。 现在我想在listview中找到如下搜索框:https://lh5.googleusercontent.com/-0H2RUBsLIYQ/Th1lqLCn5iI/AAAAAAAAALg/QZe8a5-PYu0/custom_listview_search2.png>

这是我的活动:

package de.bodprod.rettinfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class AntidotList extends Activity{
    ArrayList<HashMap<String, String>> antidotList;

    String[] sqliteIds;

    public static String TAG_ID = "id";
    public static String TAG_TOX = "tox";
    public static String TAG_ANTIDOT = "antidot";

    ListView lv;
    int textlength=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.antidotlist_layout);

        antidotList = new ArrayList<HashMap<String, String>>();
        lv = (ListView) findViewById(R.id.antidotlistlayout);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });        
        new loadStoreAntidots().execute();
    }

    class loadStoreAntidots extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... args) {
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                    List<AntidotsClass> antidots = db.getAllAntidots();
                    sqliteIds = new String[antidots.size()];

                    for (int i = 0; i < antidots.size(); i++) {

                        AntidotsClass s = antidots.get(i);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_ID, s.getId().toString());
                        map.put(TAG_TOX, s.getTox());
                        map.put(TAG_ANTIDOT, s.getAntidot());

                        // adding HashList to ArrayList
                        antidotList.add(map);

                        // add sqlite id to array
                        // used when deleting a website from sqlite
                        sqliteIds[i] = s.getId().toString();
                    }
                    ListAdapter adapter = new SimpleAdapter(
                        AntidotList.this,
                        antidotList, R.layout.antidotlistitem_layout,
                        new String[] { TAG_ID, TAG_TOX, TAG_ANTIDOT },
                        new int[] { R.id.sqlite_id, R.id.tox_layout, R.id.antidot_layout }
                    );
                    lv.setAdapter(adapter);
                }
            });
            return null;
        }
    }
}

这是我的布局:

<?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"
    style="@style/mainView" 
    android:orientation="vertical" >
    <EditText android:id="@+id/EditText01"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:hint="Suchen">                               
    </EditText>    
    <ListView
        android:id="@+id/antidotlistlayout"
        style="@style/mainView" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>           
</LinearLayout>  

我不知道如何构建搜索功能。

希望你能帮助我

1 个答案:

答案 0 :(得分:1)

  

浏览以下链接

Link1

Link2