0

私のコードは次のとおりですArrayList<>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>値を 1 つだけ取得したい場合は使用しないと思います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 に答える