【问题标题】:android sqlite listview shows packageandroid sqlite listview 显示包
【发布时间】:2014-03-04 18:42:51
【问题描述】:

我目前正在使用以下查询从我的 sqlite 数据库中提取数据:

public List<Catalog> getAllExercise(String ex_class, String period, int age1, int age2, int distance1, int distance2, String vo2max, String language) {
    List<Catalog> exerciseList = new ArrayList<Catalog>();

    String selectQuery = null;
    selectQuery = "SELECT catalog.ex_id, translation.ex_desc, translation.trans_id FROM " + TABLE_CATALOG +", " + TABLE_TRANSLATION + " WHERE catalog.ex_id = translation.ex_id AND catalog.ex_class = " + ex_class + " AND (catalog.age BETWEEN " + age1 + " AND " + age2 + ") AND (catalog.pr_comp_distance BETWEEN " + distance1 + " AND " + distance2 + ") AND translation.language = " + language + ";";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if(cursor.moveToFirst()) {
        do {
            Catalog exercise = new Catalog();
            exercise.setId(cursor.getInt(cursor.getColumnIndex(KEY_EX_ID)));
            exercise.setExDesc(cursor.getString(cursor.getColumnIndex(KEY_EX_DESC)));

            exerciseList.add(exercise);
        } while (cursor.moveToNext());
    }

    return exerciseList;
}

从我的数据库中选择 ID 和描述字段非常简单。

现在我想在我的 onCreate 函数中使用以下代码在列表视图中显示它:

DatabaseHelper db = new DatabaseHelper(getApplicationContext());

ListView list_exercises = (ListView)findViewById(R.id.list_training_sessions);

List<Catalog> exercise = new ArrayList<Catalog>();
exercise = db.getAllExercise(ex_class, period, age1, age2, distance1, distance2, vo2max, "'" + language + "'");
ArrayAdapter<Catalog> adapter = new ArrayAdapter<Catalog>(this, android.R.layout.simple_expandable_list_item_1, exercise);

list_exercises.setAdapter(adapter);

我在我的列表中得到了适量的结果,但它只显示如下:

info.olbrecht.sqlite.model.Catalog@41000dd0

我猜这是我的包名?

PS:如果我使用Log.d 会显示正确的值,那么我猜我的 listView 或转换为 String 的某个地方一定犯了错误?不太确定...

我做错了什么?

提前谢谢

【问题讨论】:

    标签: android sqlite listview


    【解决方案1】:

    ArrayAdapter 使用toString() 将对象转换为字符串表示形式。您必须在 Catalog 中覆盖 toString() 才能获得所需的输出。默认的Object.toString() 输出完整的类名、@ 符号和对象哈希码。

    【讨论】:

    • 感谢您的回答,但我不知道该怎么做。你能帮帮我吗?
    • 我不知道你的Catalog 有什么数据,但一般来说它类似于@Override public String toString() { return "some string representation"; }
    • 目录只是我所有的 getter 和 setter 以及 public Catalog( bunch of variables ) { ... }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    相关资源
    最近更新 更多