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
本主題所含的標籤
作者 請教關於將SQLite資料放入ListView中的問題
罪惡感





發文: 13
積分: 0
於 2013-10-03 10:40 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
請問一下 我想將SQLite資料取出以ListView顯示出來,我是使用SimpleCursorAdapter的方式吃Cursor,但是都會出錯,我也確定我資料表及資料庫中是有資料的不知道我的程式碼中哪邊有出問題

MainActivity.java
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.example.whattoeatnow;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
 
public class MainActivity extends Activity {
 
  private ImageButton breakfast_img_btn, lunch_img_btn, dinner_img_btn,
      midnightsnack_img_btn, dice_img_btn, add_img_btn;
  private ListView listView;
  private short status = 0;
  WhatToEatNowDB dbHlp = new WhatToEatNowDB(this);
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findView();
  }
 
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
 
  public void findView() {
 
    breakfast_img_btn = (ImageButton) findViewById(R.id.breakfast_img_btn);
    lunch_img_btn = (ImageButton) findViewById(R.id.lunch_img_btn);
    dinner_img_btn = (ImageButton) findViewById(R.id.dinner_img_btn);
    midnightsnack_img_btn = (ImageButton) findViewById(R.id.midnightsnack_img_btn);
    dice_img_btn = (ImageButton) findViewById(R.id.dice_img_btn);
    add_img_btn = (ImageButton) findViewById(R.id.add_img_btn);
    listView = (ListView)findViewById(R.id.listView);
    
    breakfast_img_btn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        // 當早餐按鈕被按下
        status = 0;
        Cursor cursor = dbHlp.QueryDB(status);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this,android.R.layout.simple_expandable_list_item_2, cursor,new String [] { "name", "phone" },
        new int []{ android.R.id.text1, android.R.id.text2},0);
        listView.setAdapter(adapter);
        }
    });
    lunch_img_btn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        // 當午餐按鈕被按下
        status = 1;
      }
    });
    dinner_img_btn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        // 當晚餐按鈕被按下
        status = 2;
      }
    });
    midnightsnack_img_btn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        // 當消夜按鈕被按下
        status = 3;
      }
    });
    dice_img_btn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
 
      }
    });
    add_img_btn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        // 當新增按鈕被按下
        Intent add = new Intent(MainActivity.this, add_interface.class);
        startActivity(add);
      }
    });
  }
  
  @Override
  public void onResume() {
    super.onResume();
    if(dbHlp == null)
      dbHlp = new WhatToEatNowDB(this); 
  }
 
  @Override
  public void onPause() {
    super.onPause();
    if(dbHlp != null){
      dbHlp.close(); 
      dbHlp = null;
    }
  }
}


WhatToEatNowDB.java
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
package com.example.whattoeatnow;
 
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
 
public class WhatToEatNowDB extends SQLiteOpenHelper {
  private static final String DATABASE_NAME = "WhatToEatNowDB.db";
  private static final int DATABASE_VERSION = 1;
  private String TABLE_NAME[] = { "breakfast", "lunch", "dinner","midnightsnack" };
  SQLiteDatabase db;
  
  public WhatToEatNowDB(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
 
  @Override
  public void onCreate(SQLiteDatabase db) {
    for (int i = 0; i < TABLE_NAME.length; i++) {
      String TABLE_CREATE = "create table if not exists "
          + TABLE_NAME[i]
          + " ( _id integer primary key autoincrement, name text not null , phone text , address text , favorite text, rating real );";
      db.execSQL(TABLE_CREATE);
      
      ContentValues cv =new ContentValues();
      
    }
  }
 
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  }
 
  public void InsertDB(short i,ContentValues cv) {
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_NAME[i], null, cv);
  
  }
 
  public void UpdateDB(short i) {
    
  }
 
  public void DeleteDB(short i) {
 
  }
 
  public Cursor QueryDB(short i) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM "+TABLE_NAME[i], null);
    db.close();
  return cursor;
  }
 
}
 


add_interface.java
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.example.whattoeatnow;
 
import android.app.Activity;
import android.content.ContentValues;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.Toast;
 
public class add_interface extends Activity {
  
  private ImageButton back_btn;
  private EditText New_Name_edt,New_Phone_edt,New_addr_edt,New_fav_edt;
  private RatingBar New_rating;
  private Button add_btn;
  private Spinner table_spinner;                                                                      
  private ArrayAdapter<String> tableList;                                                       
 
  private String[] table = {"早餐", "午餐", "晚餐", "消夜"};
  WhatToEatNowDB dbHlp;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_interface);
    findView();
  }
 
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
 
  public void findView() {
    
    
    back_btn = (ImageButton)findViewById(R.id.back_btn);
    New_Name_edt = (EditText)findViewById(R.id.New_Name_edt);
    New_Phone_edt= (EditText)findViewById(R.id.New_Phone_edt);
    New_addr_edt= (EditText)findViewById(R.id.New_addr_edt);
    New_fav_edt= (EditText)findViewById(R.id.New_fav_edt);
    New_rating =(RatingBar)findViewById(R.id.New_rating);
    add_btn = (Button)findViewById(R.id.add_btn);
    table_spinner = (Spinner)findViewById(R.id.table_spinner); 
    
    tableList = new ArrayAdapter<String>(add_interface.this,android.R.layout.simple_spinner_item,table);
    table_spinner.setAdapter(tableList);
    
    dbHlp = new WhatToEatNowDB(this);
    
    back_btn.setOnClickListener(new OnClickListener(){
      @Override
      public void onClick(View arg0) {
        finish();
      }
    });
    
    add_btn.setOnClickListener(new OnClickListener(){
      public void onClick(View arg0) {  
        
        
        if((New_Name_edt.getText().toString()).equals("")){
          Toast.makeText(add_interface.this, "店家名稱尚未輸入", Toast.LENGTH_SHORT).show();
        }
        else{
          ContentValues cv = new ContentValues();
          short table_id = (short)table_spinner.getSelectedItemId();
          cv.put("name", New_Name_edt.getText().toString());
          cv.put("phone","tel:"+New_Phone_edt.getText().toString());
          cv.put("address", New_addr_edt.getText().toString());
          cv.put("favorite", New_fav_edt.getText().toString());
          cv.put("rating", New_rating.getRating());
          dbHlp.InsertDB(table_id, cv);
          Toast.makeText(add_interface.this, "新增完成", Toast.LENGTH_SHORT).show();
        }
      }
    });
    
    
  }
  @Override
  public void onResume() {
    super.onResume();
    if(dbHlp == null)
      dbHlp = new WhatToEatNowDB(this); 
  }
 
  @Override
  public void onPause() {
    super.onPause();
    if(dbHlp != null){
      dbHlp.close(); 
      dbHlp = null;
    }
  }
}


以下是錯誤訊息
Log.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
10-02 22:32:06.473: E/AndroidRuntime(2921): FATAL EXCEPTION: main
10-02 22:32:06.473: E/AndroidRuntime(2921): java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:962)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:599)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:348)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:834)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:144)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.support.v4.widget.CursorAdapter.getCount(CursorAdapter.java:202)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.widget.ListView.setAdapter(ListView.java:463)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at com.example.whattoeatnow.MainActivity$1.onClick(MainActivity.java:57)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.view.View.performClick(View.java:4240)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.view.View$PerformClick.run(View.java:17721)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.os.Handler.handleCallback(Handler.java:730)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.os.Handler.dispatchMessage(Handler.java:92)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.os.Looper.loop(Looper.java:137)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at android.app.ActivityThread.main(ActivityThread.java:5103)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at java.lang.reflect.Method.invokeNative(Native Method)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at java.lang.reflect.Method.invoke(Method.java:525)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-02 22:32:06.473: E/AndroidRuntime(2921):   at dalvik.system.NativeStart.main(Native Method)


如果我將Main_Activity.java中的listView.setAdapter(adapter);註解掉,那程式是可以執行的,我以我目前猜測可能我的資料輸入時有發生錯誤,麻煩各位幫我看一下我的程式有哪邊出錯


reply to postreply to post
作者 Re:請教關於將SQLite資料放入ListView中的問題 [Re:罪惡感]
罪惡感





發文: 13
積分: 0
於 2013-10-03 11:38 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
已解決

reply to postreply to post
作者 Re:請教關於將SQLite資料放入ListView中的問題 [Re:罪惡感]
fsz570





發文: 19
積分: 0
於 2013-10-03 11:39 user profilesend a private message to usersend email to fsz570reply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
會是 WhatToEatNowDB.QueryDB() 裡的 db.close() 造成的嗎?

reply to postreply to post

David Wu(吳清霖)
E-mail: fsz570@gmail.com

世事短如春夢,人情薄似秋雲。 不須計較苦勞心。萬事原來有命。
幸遇三杯酒好,況逢一朵花新。 片時歡笑且相親。明日陰晴未定。
» 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