0

创建可搜索的联系人类型的应用程序。下面是我的代码

public class EmployeeList extends Activity {

    protected EditText searchText;
    protected SQLiteDatabase db;
    protected Cursor cursor, listcursor;
    protected ListAdapter adapter;
    protected ListView employeeList;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        db = (new DatabaseHelper(this)).getWritableDatabase();
        searchText = (EditText) findViewById (R.id.searchText);
        employeeList = (ListView) findViewById (R.id.list);

        listcursor = db.rawQuery("SELECT * FROM employee",null);

        adapter = new SimpleCursorAdapter(
                this, 
                R.layout.employee_list_item, 
                listcursor, 
                new String[] {"firstName", "lastName", "title"}, 
                new int[] {R.id.firstName, R.id.lastName, R.id.title});
        employeeList.setAdapter(adapter);
    }

        public void search(View view) {
            // || is the concatenation operation in SQLite
                    cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee WHERE firstName || ' ' || lastName LIKE ?", 
                                                    new String[]{"%" + searchText.getText().toString() + "%"});
                    adapter = new SimpleCursorAdapter(
                                    this, 
                                    R.layout.employee_list_item, 
                                    cursor, 
                                    new String[] {"firstName", "lastName", "title"}, 
                                    new int[] {R.id.firstName, R.id.lastName, R.id.title});
                    employeeList.setAdapter(adapter);

                                        }

}

问题出在从编辑文本中清除单词之后。它应该回到以前的列表视图,即它应该显示数据库中的所有值。

在此处输入图像描述

4

1 回答 1

2

而不是使用第二个光标,您应该使用带有 EditText 的FilterQueryProvider来更新对 ListView 执行多个更新,就像 AutoCompleteTextView 一样。

或者您可以添加一个 TextWatcher 并在长度变为零时将光标更改回您的第一个。

于 2012-05-08T05:53:43.897 回答