联系人查询 - 虽然程序有0个错误但没有执行

时间:2012-02-14 11:25:30

标签: android

我正在研究样本数据库,如何通过程序搜索联系人。 这是我的java文件和XML文件。

EmployeeList.java

package samples.employeedirectory;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class EmployeeList extends Activity {

    protected EditText searchText;
    protected SQLiteDatabase db;
    protected Cursor cursor;
    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);
    }

    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);
    }

}

**DatabaseHelper.java**


package samples.employeedirectory;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

        public static final String DATABASE_NAME = "employee_directory";

        public DatabaseHelper(Context context) {
                super(context, DATABASE_NAME, null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
                /*
                 * Create the employee table and populate it with sample data.
                 * In step 6, we will move these hardcoded statements to an XML document.
                 */
                String sql = "CREATE TABLE IF NOT EXISTS employee (" +
                                                "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                                                "firstName TEXT, " +
                                                "lastName TEXT, " +
                                                "title TEXT, " +
                                                "officePhone TEXT, " +
                                                "cellPhone TEXT, " +
                                                "email TEXT, " +
                                                "managerId INTEGER)";
                db.execSQL(sql);

                ContentValues values = new ContentValues();

                values.put("firstName", "John");
                values.put("lastName", "Smith");
                values.put("title", "CEO");
                values.put("officePhone", "617-219-2001");
                values.put("cellPhone", "617-456-7890");
                values.put("email", "jsmith@email.com");
                db.insert("employee", "lastName", values);

                values.put("firstName", "Robert");
                values.put("lastName", "Jackson");
                values.put("title", "VP Engineering");
                values.put("officePhone", "617-219-3333");
                values.put("cellPhone", "781-444-2222");
                values.put("email", "rjackson@email.com");
                values.put("managerId", "1");
                db.insert("employee", "lastName", values);

                values.put("firstName", "Marie");
                values.put("lastName", "Potter");
                values.put("title", "VP Sales");
                values.put("officePhone", "617-219-2002");
                values.put("cellPhone", "987-654-3210");
                values.put("email", "mpotter@email.com");
                values.put("managerId", "1");
                db.insert("employee", "lastName", values);

                values.put("firstName", "Lisa");
                values.put("lastName", "Jordan");
                values.put("title", "VP Marketing");
                values.put("officePhone", "617-219-2003");
                values.put("cellPhone", "987-654-7777");
                values.put("email", "ljordan@email.com");
                values.put("managerId", "2");
                db.insert("employee", "lastName", values);

                values.put("firstName", "Christophe");
                values.put("lastName", "Coenraets");
                values.put("title", "Evangelist");
                values.put("officePhone", "617-219-0000");
                values.put("cellPhone", "617-666-7777");
                values.put("email", "ccoenrae@adobe.com");
                values.put("managerId", "2");
                db.insert("employee", "lastName", values);

                values.put("firstName", "Paula");
                values.put("lastName", "Brown");
                values.put("title", "Director Engineering");
                values.put("officePhone", "617-612-0987");
                values.put("cellPhone", "617-123-9876");
                values.put("email", "pbrown@email.com");
                values.put("managerId", "2");
                db.insert("employee", "lastName", values);

                values.put("firstName", "Mark");
                values.put("lastName", "Taylor");
                values.put("title", "Lead Architect");
                values.put("officePhone", "617-444-1122");
                values.put("cellPhone", "617-555-3344");
                values.put("email", "mtaylor@email.com");
                values.put("managerId", "2");
                db.insert("employee", "lastName", values);

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                db.execSQL("DROP TABLE IF EXISTS employees");
                onCreate(db);
        }

}

and my XML files are,

**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" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/searchText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:id="@+id/searchButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search" />
    </LinearLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

**employee_list_item.xml**

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

        <TextView
                android:id="@+id/firstName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
                android:id="@+id/lastName"
                android:layout_marginLeft="6px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/firstName"/>

        <TextView
                android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/firstName"/>

</RelativeLayout>

运行应用程序后,当我单击搜索按钮时,不会显示任何结果(没有任何结果)。

感谢所有帮助我的人,因为我是Android技术的新手。

1 个答案:

答案 0 :(得分:1)

您需要为按钮添加一个监听器,该监听器将在点击时调用搜索:

Button b = (Button) findViewById(R.id. searchButton);
b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        search(v);
    }
});