我从sqlite获取对象数据输出而不是我需要的数据

时间:2013-04-05 15:50:01

标签: android sqlite

我在这里从我的代码中获取对象数据,但我不知道如何做到这一点。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manage);

    Log.d(TAG, "onCreate");

    datasource = new DataSource(this);
    datasource.open();

    List<Encouragement> values = datasource.getAllEncouragements();

    Log.d(TAG, "values");

    ArrayAdapter<Encouragement> adapter = new ArrayAdapter<Encouragement>(this,
        android.R.layout.simple_list_item_1, values);

    setListAdapter(adapter);
    Log.d(TAG, "end of onCreate");
}

public List<Encouragement> getAllEncouragements() {

    List<Encouragement> encouragements = new ArrayList<Encouragement>();

    Cursor cursor = database.query(DatabaseHelper.TABLE_ENCOURAGEMENTS,
            allColumns, null, null, null, null, null);

    cursor.moveToFirst();

    while (!cursor.isAfterLast()) {
        Encouragement encouragement = cursorToEncouragement(cursor);
        encouragements.add(encouragement);
        cursor.moveToNext();
    }

    cursor.close();
    return encouragements;
}

在列表项中生成 com.example.myActivity.myObject@34563475

1 个答案:

答案 0 :(得分:1)

listview正在调用toString()来表示myObject。您尚未覆盖toString()方法,因此会调用默认的Object.toString()方法。

@Override
public String toString(){
   return this.name; // whatever you need to represent this instance of myObject.
}