1

我正在为 android 构建一个字典应用程序。我想创建一个搜索按钮,可以搜索 SQLite 数据库,然后将数据显示到 ListView。

  1. 如何编码以便我将该搜索按钮链接到数据库 SELECT 语句?
  2. 如何将 SELECT 语句结果显示到 ListView?

提前致谢。

4

2 回答 2

0

为此,您需要根据搜索栏的内容创建查询,例如:

select * from <your_database> where word = "<word typed in the search field>"

即,您必须创建一个基本字符串,例如select * from <your_database> where word = ",然后在按下“选择”按钮时将其与搜索栏的内容连接起来。

另外你想用 wordlike "#<word typed in the search field>#"

并拆分单词并检测相似的单词并在数据库中搜索它们,这特定于您正在谈论的实现,即字典。我的意思是,例如,如果我输入cavalier字典,字典结果也包括cavalier hat,但这些取决于您的目标。

是一个android的数据库教程。

应该可以帮助您使用ListView

于 2012-05-15T19:48:12.830 回答
0

为了与您打交道,ListView可以使用我的代码它做同样的事情,即从文本字段获取输入,在 db 中搜索并打印结果ListView

    EditText userInput = (EditText)findViewById(R.id.myEditText);
    SQLiteClass mySQLiteClass = new SQLiteClass(this);
    mySQLiteClass.openToWrite();
    Cursor cursor = mySQLiteClass.returnResultsFromDb(userInput.getText().toString);

    ArrayList<String> result = new ArrayList();
    for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()) {
       result.add(cursor.getString(cursor.getColumnIndex(SQLiteClass.BI_Attr2)));
    }

    String[] Values = new String[result.size()];

    for (int i=0; i<result.size(); i++) {
        Values[i] = result.get(i);
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_single_choice, android.R.id.text1, Values);

    ListView LV = (ListView)findViewById(R.id.myListView);
    LV.setAdapter(adapter);

并遵循我的数据库类

public class SQLiteClass{

    public static final String MYDATABASE_NAME = "DATABASE_Proj";
    public static final String MYDATABASE_TABLE = "Basic_Info";
    public static final String BI_Attr1 = "ID"; // Primary Key
    public static final String BI_Attr2 = "Name";
    public static final String BI_Attr3 = "StudentRoll";
    public static final String BI_Attr4 = "FromCourse";
    public static final String BI_Attr5 = "ToCourse";
    public static final String BI_Attr6 = "Time";
    public static final String BI_Attr7 = "Status";
    public static final String BI_Attr8 = "Comments";


    private static final String SCRIPT_CREATE_DATABASE = "create table "
        + MYDATABASE_TABLE + 
        " (" + BI_Attr1         + " integer primary key autoincrement, " +
        BI_Attr2 + " text not null, " +
        BI_Attr3 + " text," +
        BI_Attr4 + " text," + 
        BI_Attr5 + " text," +
        BI_Attr6 + " text," +
        BI_Attr7 + " text," +
        BI_Attr8 + " text " +

        "); ";


    public static final int MYDATABASE_VERSION = 1;

    private SQLiteHelper sqLiteHelper;
    private SQLiteDatabase sqLiteDatabase;

    private Context context;

    public SQLiteClass(Context c) {
        context = c;
    }

    public SQLiteClass openToRead() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
                MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getReadableDatabase();
        return this;
    }

    public SQLiteClass openToWrite() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
                MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        sqLiteHelper.close();
    }

    public Cursor returnResultsFromDb(String S) {
        String[] columns = new String[] { BI_Attr3 };
        Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null,
            null, null, null, null);
        return cursor;
    }

    public class SQLiteHelper extends SQLiteOpenHelper {

        public SQLiteHelper(Context context, String name,
                CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            try {
                db.execSQL(SCRIPT_CREATE_DATABASE);
            } catch (Exception E) {
                Log.v("Failed to create DB", "----");
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
    }
}
于 2012-05-15T20:23:16.303 回答