0

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.

4

2 回答 2

2

List<E>如果您只想从中获得一个值,我认为您不要使用,SQLite因此您应该尝试这个!

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;
    ^^^^^^^^^^^^^^^^     
}

并在onCreate()使用中:

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());
                     ^^^^^^^^^^^^^^^^^^^^^^^^^
}

我希望这对你有用!

于 2013-03-06T15:38:50.323 回答
0

您的 contentDataObject 从未初始化,因此您实际上是在调用。

 textView.setText(""+contentDataObjects.indexOf( NULL ));
于 2013-03-06T10:13:08.483 回答