2011-11-17 61 views
0

我一直在努力解決這個問題,但似乎無法找到我在尋找的搜索谷歌過去幾天,我打了,錯過了東西,是傾向於我想做的事情,但沒有碰到任何我想做的事情。我開始建立我的SQLite數據庫以及「TheNewBoston Android Tuts」一切正常,現在我試圖改變視圖數據庫周圍的錯誤,或者在啓動時崩潰應用程序。我想要使​​用for循環來顯示數據庫中某行中的某些列,並使用for循環來動態加載TableView:Android SQLite:顯示數據條目

DbEntry [data |數據|顯示|數據|顯示|顯示|數據]

我可以做到這一點,如果我有一個自己的TextView關閉,但與它在TableLayout> TableRow我認爲這是問題的地方,但不知道...希望能夠加載錶行如果可能的話,從類自身而不是設置文本視圖。我希望我給了我想要做的足夠的信息,如果任何人都可以指向我一個非常好的教程,或者對此有所瞭解,那麼我將非常感激。

XML:

<TableLayout 
    android:id="@+id/view_Table" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="5" > 
<TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="4" > 
     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" > 
      <TextView 
       android:id="@+id/view_DbCol3" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="DB Col 3 Displayed Data" /> 
     </TableRow> 

與XML我希望我通過把錶行於側彼此

SQLview類

setContentView(R.layout.sqlview); 
    TextView tv = (TextView) findViewById(R.id.view_DbCol3); 
    try { 
     DBload info = new DBload(this); 
     info.open(); 
     String data = info.getData(); 
     info.close(); 
     tv.setText(data); 
    } catch (Exception e) { 
     // Error Message Dialog 
     String error = e.getLocalizedMessage(); 
     Dialog d = new Dialog(this); 
     d.setTitle("DB ERROR"); 
     TextView tv1 = new TextView(this); 
     tv1.setText(error); 
     d.setContentView(tv1); 
     d.show(); 
    } 
做右表,它類似於HMTL樣的東西

DbHelper類:GetData方法

public String getData() { 

    String[] cols = new String[] {KEY_PLACE}; 
       // I have also tried with all my KEY variables 

    // reads info from DB columns 
    Cursor c = ourDB.query(KEY_TABLE, cols, null, null, null, null, null); 
    String result = ""; 

    int Ione = c.getColumnIndex(KEY_PLACE); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
     result = result 
     + c.getString(Ione) 
     + "\n"; 
    } 
    return result; 
} 

我正的誤差是

11-17 00:13:09.993:E/CursorWindow(590):爲字段時隙0爲請求,-1。 = numRows行2,爲numColumns = 1

就像我說的,我希望我已經給了足夠的信息,幫助不大,如果可能的話,非常感謝

+0

只需使用listview ...在sdk文件夾中找到示例... NotePad項目或ApiDemos中的List7.java – Selvin

回答

1

link將幫助你很多。

anywayz試試這個在您的代碼

if(c.getCount()>0) { 
    int index=0; 
    while(c.moveToNext()){ 

    result = result 
      + c.getString(index) 
      + "\n"; `enter code here` 
    index++; 
    } 
} 

希望這有助於..

+0

您需要更改c的變量名稱,如同使用整數數據類型定義的,而c已經與Cursor數據類型一起使用 – Pratik

1

的錯誤似乎是說,你的列「KEY_PLACE」不存在,因爲器的columnIndex方法返回-1。

檢查KEY_PLACE的價值和列的名稱返回在你的光標

希望它可以幫助 Jokahero

PS:你應該檢查你的光標不是null使用它之前:

if (c != null) { 
    while (c.moveToNext()) { 
     // ... 
    } 
} 

而且也不要忘記關閉你的光標當作業完成:

c.close(); 
+0

非常感謝,,,,幫助了很多,,其他兩個傢伙讓我去思考如果聲明的線路無法得到我的手指..但這修復了什麼毛刺雖然.. 。非常感謝你的幫助 – acrichm

+0

不客氣;) – Jokahero