如何从数据库中选择一列并在android textView中显示?

时间:2013-11-30 02:50:35

标签: android sql sqlite

我是android编程的新手,我尝试在文本视图中显示我的select查询的结果,我一直在尝试在过去3天内完成它,但它仍然只是不起作用;这是我的代码:

        f.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            db.open();

            Cursor c = db.getWord(find.getText().toString());
            c.moveToFirst();

            String[] from = {DBManager.KEY_ENGLISH};
            int[] to = {R.id.TVTrans};



            SimpleCursorAdapter a = new SimpleCursorAdapter(StartingPoint.this, R.layout.dic, c, from, to);

            show.setText(a.convertToString(c));

            db.close();
        }
    });

这是我的db函数,而getWord(String bahahsaIndo)中的最后一个是我想要使用的函数

public class DBManager {
public static final String KEY_ROWID = "_id";
public static final String KEY_BAHASA = "bahasa";
public static final String KEY_ENGLISH = "english";
private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "dict";
private static final String DATABASE_TABLE = "words";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE =
    "create table " + DATABASE_TABLE + " (_id integer primary key autoincrement, "
    + KEY_BAHASA + " text not null, " +  KEY_ENGLISH + " text not null" 
    + ");";

private final Context context; 

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBManager(Context ctx) 
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper 
{
    DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
    int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion 
                + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS titles");
        onCreate(db);
    }
}    

//---opens the database---
public DBManager open() throws SQLException 
{
    db = DBHelper.getWritableDatabase();
    return this;
}

//---closes the database---    
public void close() 
{
    DBHelper.close();
}

//---insert a title into the database---
public long insertTitle(String bahasaIndo, String englishLang) 
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_BAHASA, bahasaIndo);
    initialValues.put(KEY_ENGLISH, englishLang);
    return db.insert(DATABASE_TABLE, null, initialValues);
}



//---retrieves all the titles---
public Cursor getAll() 
{
    return db.rawQuery("select * from " + DATABASE_TABLE, null);
    /*return db.query(DATABASE_TABLE, new String[] {
            KEY_ROWID, 
            KEY_BAHASA, 
            KEY_ENGLISH,
            null}, 
            null, 
            null, 
            null, 
            null, 
            null);*/
}
public Cursor getWord(String bahasaIndo){
    return db.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE " + KEY_BAHASA + " = '" + bahasaIndo+"'", null);
}

}

最后,这是我的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">
    <EditText 
android:id="@+id/ETFind" 
android:layout_height="wrap_content" 
android:text="Type the word that you want to find...." 
android:layout_width="wrap_content">
    </EditText>
    <TextView 
android:text="@+id/TextView01" 
android:id="@+id/TVTrans" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
    </TextView>
    <Button 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/bFind" 
android:text="Find">
    </Button>
   <Button 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/bSpeak" 
android:text="Speak">
    </Button>

   </LinearLayout>

任何帮助都会受到关注:)

1 个答案:

答案 0 :(得分:1)

public class DBManager {
    public static final String KEY_ROWID = "_id";
    public static final String KEY_BAHASA = "bahasa";
    public static final String KEY_ENGLISH = "english";
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "dict";
    private static final String DATABASE_TABLE = "words";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS "
        _ DATABASE_TABLE + "(_id integer primary key autoincrement,"
        + KEY_BAHASA + " text not null," + KEY_ENGLISH + " text not null);";

    private final Context context;

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBManager(Context ctx) {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion
                    + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }

    public DBManager open() throws SQLException {
        db = DBHelper.getWritableDatabase();
        return this;
    }

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

    public long insertTitle(String bahasaIndo, String englishLang) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_BAHASA, bahasaIndo);
        initialValues.put(KEY_ENGLISH, englishLang);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    public Cursor getAll() {
        return db.query(DATABASE_TABLE, null, null, null, null, null, null);
    }

    public Cursor getWord(String bahasaIndo) {
        return db.query(DATABASE_TABLE, null, KEY_BAHASA + "=?", new String[] {
            bahasaIndo
        }, null, null, null);
    }
}

测试代码:

@Override
public void onClick(View v) {
    DBManager manager = new DBManager(this);
    manager.open();

    Random random = new Random();
    if (random.nextBoolean()) {
        Log.d("TAG", "Insert Value");

        manager.insertTitle("bahasaindo", "english");
        manager.insertTitle("bahasaindo" + random.nextInt(), "english1");
    } else {
        Log.d("TAG", "query value");

        Cursor cursor = manager.getWord("bahasaindo");
        if (cursor.moveToFirst()) {
            Log.d("TAG",     cursor.getString(cursor.getColumnIndex(DBManager.KEY_BAHASA)));
        }
    }
    manager.close();
}