我在这里从我的代码中获取对象数据,但我不知道如何做到这一点。
@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 。
答案 0 :(得分:1)
listview正在调用toString()
来表示myObject。您尚未覆盖toString()
方法,因此会调用默认的Object.toString()
方法。
@Override
public String toString(){
return this.name; // whatever you need to represent this instance of myObject.
}