如何从列表中获取单个值<;E>;在android中
我想从如何从列表中获取单个值<;E>;在android中,android,sqlite,cursor,android-sqlite,Android,Sqlite,Cursor,Android Sqlite,我想从SQLite获取以ArrayList形式返回的单列值,用于检索数据我的代码是: public List<ContentDataObject> findAll() { List<ContentDataObject> contentDataObjects = new ArrayList<ContentDataObject>(); String selectQuery = "SELECT " + DBHelper
SQLite
获取以ArrayList
形式返回的单列值,用于检索数据我的代码是:
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;
}
在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()));
}
contentdataobjectcontentdataobject;
文本视图文本视图;
内容数据源;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(textView)findViewById(R.id.myView);
数据源=新内容数据源(此);
dataSource.open();
List contentDataObjects=dataSource.findAll();
textView.setText(“+contentDataObjects.indexOf(contentDataObject.getFulltext()));
}
但是,我无法获取数据并将其设置为
textView
您的contentDataObject从未初始化,因此您的调用实际上是无效的
textView.setText(""+contentDataObjects.indexOf( NULL ));
如果您只想从
SQLite
获取一个值,我认为您不需要使用List
,所以您应该试试这个
通过以下方式更改findAll()
:
public String findAll() {
^^^^^^
String myQuery = "SELECT " +
DBHelper.MOBILE_CONTENT_FULLTEXT+
" FROM " + DBHelper.MOBILE_CONTENT_TABLE_NAME+
" WHERE _id = 2";
Cursor cursor = database.rawQuery(myQuery, null);
String fullText = null;
^^^^^^^^^^^^^^^
if(cursor.getCount() <= 0) {
Log.w(TAG, "There is no data to display");
}
else {
fullText = "";
if(cursor.moveToFirst()) {
do {
fullText += cursor.getString(cursor.getColumnIndex(DBHelper.MOBILE_CONTENT_FULLTEXT)) + "\n";
} while(cursor.moveToNext());
}// end if
}
return fullText;
^^^^^^^^^^^^^^^^
}
我希望这对你有用 在活动中,
contentDataObject
的初始化在哪里?-1因为不清楚什么不起作用,也不清楚您尝试了什么我现在已经初始化了我的contentDataObject
,但我仍然得到了结果-1
!如果没有匹配项,indexOf返回-1。正如您在我的url中看到的,您比较的是一个对象,而不是一个字符串。所以您应该在代码中使用(我认为):textView.setText(“+contentDataObjects.indexOf(contentDataObject));这将返回对象的索引。我不知道您想在文本视图中显示什么,如果它是全文的话:contentDataObjects.get(indexOf(contentDataObject)).getFulltext()竖起大拇指亲爱的!这就是我想做的。谢谢。
public String findAll() {
^^^^^^
String myQuery = "SELECT " +
DBHelper.MOBILE_CONTENT_FULLTEXT+
" FROM " + DBHelper.MOBILE_CONTENT_TABLE_NAME+
" WHERE _id = 2";
Cursor cursor = database.rawQuery(myQuery, null);
String fullText = null;
^^^^^^^^^^^^^^^
if(cursor.getCount() <= 0) {
Log.w(TAG, "There is no data to display");
}
else {
fullText = "";
if(cursor.moveToFirst()) {
do {
fullText += cursor.getString(cursor.getColumnIndex(DBHelper.MOBILE_CONTENT_FULLTEXT)) + "\n";
} while(cursor.moveToNext());
}// end if
}
return fullText;
^^^^^^^^^^^^^^^^
}
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();
parseAndIsertData();
textView.setText(dataSource.getFullText());
^^^^^^^^^^^^^^^^^^^^^^^^^
}