ArrayList<>
我想获取以from形式返回的单列值,SQLite
用于检索数据我的代码是:
public List<ContentDataObject> findAll() {
List<ContentDataObject> contentDataObjects = new ArrayList<ContentDataObject>();
String selectQuery = "SELECT " +
DBHelper.MOBILE_CONTENT_FULLTEXT+
" FROM " + DBHelper.MOBILE_CONTENT_TABLE_NAME;
database = dbOpenHelper.getReadableDatabase();
try {
Cursor cursor = database.rawQuery(selectQuery, null);
Log.i(TAG, "Returned " + cursor.getCount() + " rows");
if(cursor != null){
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
ContentDataObject contentDataObject = check(cursor);
contentDataObjects.add(contentDataObject);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
}
} catch (Exception e) {
// TODO: handle exception
}
return contentDataObjects;
}
我check(cursor)
的是:
public ContentDataObject check(Cursor cursor) {
ContentDataObject contentDataObject = new ContentDataObject();
contentDataObject.setId(cursor.getLong(cursor.getColumnIndex(DBHelper.MOBILE_CONTENT_ID)));
contentDataObject.setTitle(cursor.getString(cursor.getColumnIndex(DBHelper.MOBILE_CONTENT_TITLE)));
contentDataObject.setFulltext(cursor.getString(cursor.getColumnIndex(DBHelper.MOBILE_CONTENT_FULLTEXT)));
contentDataObject.setState(cursor.getInt(cursor.getColumnIndex(DBHelper.MOBILE_CONTENT_STATE)));
contentDataObject.setNewValue(cursor.getInt(cursor.getColumnIndex(DBHelper.MOBILE_CONTENT_NEW)));
contentDataObject.setHeader(cursor.getString(cursor.getColumnIndex(DBHelper.MOBILE_CONTENT_HEADER)));
contentDataObject.setColor(cursor.getInt(cursor.getColumnIndex(DBHelper.MOBILE_CONTENT_COLOR)));
contentDataObject.setNext(cursor.getString(cursor.getColumnIndex(DBHelper.MOBILE_CONTENT_NEXT)));
contentDataObject.setPrevious(cursor.getString(cursor.getColumnIndex(DBHelper.MOBILE_CONTENT_PREVIOUS)));
return contentDataObject;
}
在MainActivity.java
我使用这个代码:
ContentDataObject contentDataObject;
TextView textView;
ContentsDataSource dataSource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.myView);
dataSource = new ContentsDataSource(this);
dataSource.open();
List<ContentDataObject> contentDataObjects = dataSource.findAll();
textView.setText(""+contentDataObjects.indexOf(contentDataObject.getFulltext()));
}
但是,我无法获取数据并设置为textView
.