JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java ME、Google Android 平台與 JavaCard 討論區 » Android  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 將資料庫內容顯示到ListView上
調皮搗蛋





發文: 7
積分: 0
於 2009-12-26 17:51 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
最近練習了存取資料庫的範例,但是一直出現錯誤
我建立了一個DB的類別,也將table都寫入了資料庫
但是要將資料庫內容傳給Cursor c存取後,用SimpleCursorAdapter將資料庫內容呈現出來,
卻出現了錯誤

這是DB的類別
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.demo.android.phonebook;
 
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
 
public class PhoneBookDB {
  private static final String DATABASE_NAME = "phonebook_db";
  private static final int DATABASE_VERSION = 1;
  private static final String TABLE_NAME = "phonebook_table";
  private static final String FIELD_id = "id_";
  private static final String FIELD_name = "phonebook_name";
  private static final String FIELD_phone_number = "phonebook_number";
  private static final String FIELD_address = "phonebook_address";
  private static final String DATABASE_CREATE = 
    "create table phonebook_table("
    +"id_ INTEGER PRIMARY KEY,"
    +"phonebook_name TEXT,"
    +"phonebook_number INTEGER,"
    +"phonebook_address TEXT"
    +");";
  String[] strCol = new String[]{FIELD_id, FIELD_name, FIELD_phone_number, FIELD_ad  dress};
  
  private Context cCtx = null;
  private DatabaseHelper dbHelper;
  private SQLiteDatabase db;
 
  
  private static class DatabaseHelper extends SQLiteOpenHelper{
    
    public DatabaseHelper(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
      // TODO Auto-generated constructor stub
      
    }
 
    @Override
    public void onCreate(SQLiteDatabase db) {
      // TODO Auto-generated method stub
      db.execSQL(DATABASE_CREATE);
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      // TODO Auto-generated method stub
      db.execSQL("DROP TABLE IF EXITS " + TABLE_NAME);
      onCreate(db);
    }
    
  }
  
  public PhoneBookDB(Context ctx){
    this.cCtx = ctx;
  }
  
  /*Open DB*/
  public PhoneBookDB open() throws SQLException{
    dbHelper = new DatabaseHelper(cCtx);
    db = dbHelper.getWritableDatabase();
    return this;
  }
  
  /*Close DB*/
  public void close(){
    dbHelper.close();
  }
   /*get all entries*/
  public Cursor getall(){
    Cursor c = db.query(TABLE_NAME, new String[]{FIELD_name}, null, null, null, null, null);
    return c;  
  }
 
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
這是主程式類別
 
package com.demo.android.phonebook;
 
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
 
public class PhoneBook extends ListActivity {
  private PhoneBookDB mDB;
  private Cursor mC;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        getListView().setEmptyView(findViewById(R.id.tv02));
        setAdapter();
    }
    
    private void setAdapter(){
      mDB = new PhoneBookDB(this);
      mDB.open();
      fillData();
      mDB.close();
    }
    
    private void fillData(){
      
         mC  = mDB.getall();
          
          startManagingCursor(mC);
          
          String[] from = new String[]{"phonebook_name"};
          int[] to = new int[]{android.R.id.text1};
          
          SimpleCursorAdapter adapter = 
            new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, mC, from, to);
          
          setListAdapter(adapter);
        
    }
}


在我利用getall()方法將資料庫內容提取出來利用mC存取,但是在宣告SimpleCursorAdapter接口將
mC用List表示出來時卻出現錯誤,若是在宣告SimpleCursorAdapter時候將mC用null換掉就不會出現runtime error,當然資料庫的內容也沒傳至list中
請問各位大大為什麼有這樣的問題呢?我的資料表也都建立好了,裡面也建立了一筆資料,應該不至於會因為沒有資料而出現錯誤,而是存取資料庫內容的cursor出現了問題??

藍色匡匡框起來的錯誤是說我當初建立資料庫時把欄位_id不小心錯打成id_,但是我現在都把_id改成id_了,卻還是出現這樣的錯誤,讓我相當的不解,請問各位大大能幫我解決這個問題嗎??


reply to postreply to post
作者 Re:將資料庫內容顯示到ListView上 [Re:調皮搗蛋]
elbe





發文: 13
積分: 0
於 2010-01-06 11:09 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
將程式uninstall在執行一次

reply to postreply to post
作者 Re:將資料庫內容顯示到ListView上 [Re:elbe]
gasolin





發文: 149
積分: 1
於 2010-01-06 14:30 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
SimpleCursorAdapter 裡預設用 _id 當 index,
所以要用 SimpleCursorAdapter 的話,建 db 要遷就這個配置


reply to postreply to post
» JWorld@TW »  Java ME、Google Android 平台與 JavaCard 討論區 » Android

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8