2011-12-01 101 views
0

我正在與其他兩個人一起工作一個項目,並且您可能猜測它有點與某人elses代碼一起工作。我們正在開發的應用程序從互聯網獲取信息,每個項目包含標題,鏈接,帖子和日期。這個信息被讀入一個遊標,然後在一個字符串緩衝區中輸出。如果你想在模擬器窗口中整齊地顯示信息,這顯然沒有用處。我希望有人能夠幫助我理解如何使用這些信息來使其易於使用(即在一個TextView中的每個條目以及列表視圖中的所有條目)。從數據庫填充ListView與光標信息,Android應用

private void showEvents() { 
    SQLiteDatabase db = post.getWritableDatabase(); 
    Cursor cursor= db.rawQuery("SELECT "+TITLE+", "+LINK+", "+POST+", "+DATE+" FROM "+TABLE_NAME+" "+"ORDER BY "+DATE+" DESC;",null); 
    startManagingCursor(cursor); 
    TextView tv = new TextView(this); 

    // Stuff them all into a big string 
    StringBuilder builder = new StringBuilder(""); 


    while (cursor.moveToNext()) { 
     String title = cursor.getString(0); 
     String link = cursor.getString(1); 
     String post = cursor.getString(2); 
     String date= cursor.getString(3); 

     builder.append(title).append(": "); 
     builder.append(link).append(": "); 
     builder.append(post).append(": "); 
     builder.append(date).append("\n"); 

    } 


    // Display on the screen 
    tv.setText(builder); 
    this.setContentView(tv); 
} 

這段代碼基本上只輸出一切,未格式化到屏幕上。我一直在想如何進入while循環來捕獲每個實體(這是標題,鏈接,帖子,日期1)並從那裏開始。

任何意見表示讚賞。

回答

0

我建議你讀一本書。將涵蓋這些主題的內容是Manning Publications出版的「Android實踐」。

對此有幾個重要的必需元素,但關鍵概念是「Adapter」,它將視圖與數據連接起來。您最終將定義一個POJO來表示每條記錄中的數據,並且您的自定義適配器將從該POJO映射到視圖字段中。

0

你進入這個while循環每一次,你正在讀另一行(因爲使用MoveToNext())的

while (cursor.moveToNext()) { 
    String title = cursor.getString(0); 
    String link = cursor.getString(1); 
    String post = cursor.getString(2); 
    String date= cursor.getString(3); 

    builder.append(title).append(": "); 
    builder.append(link).append(": "); 
    builder.append(post).append(": "); 
    builder.append(date).append("\n"); 

} 
相關問題