-2

数据库.java

public class Data extends SQLiteOpenHelper {
public static String week, classes, title, mat, it, comp, que;

///更常见的代码,如创建、删除数据库

 public String[] getAllIAI(){

    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_IAI + "WHERE KEY_week =" +LessonPlanner.itemWeek+ "AND KEY_class=" +LessonPlanner.itemClass ;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor3 = db.rawQuery(selectQuery, null);
    String[] data = null;
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {

            title=cursor.getString(3);
            mat=cursor.getString(4);
            it=cursor.getString(5);
       } while (cursor.moveToNext());
    }

    // closing connection
    cursor.close();
    db.close();

    // returning lables
    return data;
}   

推荐.Java

宣布

TextView  title, mat, IT;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recomend);

    title = (TextView) findViewById(R.id.titleText);
    mat = (TextView) findViewById(R.id.materialText);
    IT = (TextView) findViewById(R.id.instTText);

    title.setText(Data.title);
    mat.setText(Data.mat);
    IT.setText(Data.it);

我认为代码在某处是错误的,因为 Textview(多行文本)没有显示任何内容。谁能指出我的错误?我对 Eclipse 完全陌生。

4

1 回答 1

0

你的代码有几个错误,首先,你返回的数组“数据”总是空的,所以你当然不能从中得到任何东西。

其次,如果您打算从数据库中获取多个结果,您应该声明一个列表并将您从数据库中获得的对象添加到其中,目前您的循环完全没用,您实例化您从未使用过的变量......

这是一个示例,展示了如何从数据库中获取列表,但您也可以获取游标、数组或任何您需要的东西:

// Getting All Categories By ID
    public List<Category> getCategoriesList() {
        String selectQuery = "SELECT " + CATEGORY_ID + ", " + CATEGORY_NAME + " FROM " + CATEGORY_TABLE + " ORDER BY " + CATEGORY_ID + " ASC";
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null); 

        List<Category> categoriesList = new ArrayList<Category>();

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Category category = new Category(cursor.getInt(0), cursor.getString(1), false);
                categoriesList.add(category);
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return categoriesList;
    }

注意:在此示例中,我从数据库中获取了几行,如果您只想获取一行,只需删除循环 do-while 并直接返回对象。

于 2013-04-13T15:20:46.270 回答