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

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

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 請問關於SQLite取出資料並顯示的問題
罪惡感





發文: 13
積分: 0
於 2013-10-06 10:57 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
各位好,我目前在做的APP需要用到SQLite取出資料並顯示在另一個Activity上,我是用ListView的方式取得使用者點擊的某筆資料庫資料,將_id當作條件,並從SQLite中找出資料顯示在另一個Activity上,程式碼如下

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
 
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);
    
    TableShow(status);
    
    breakfast_img_btn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        // 當早餐按鈕被按下
        status = 0;
        TableShow(status);
        }
    });
    lunch_img_btn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        // 當午餐按鈕被按下
        status = 1;
        TableShow(status);
      }
    });
    dinner_img_btn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        // 當晚餐按鈕被按下
        status = 2;
        TableShow(status);
      }
    });
    midnightsnack_img_btn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        // 當消夜按鈕被按下
        status = 3;
        TableShow(status);
      }
    });
    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);
      }
    });
    
    listView.setOnItemClickListener(new OnItemClickListener(){
      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
          long arg3) {
        // TODO Auto-generated method stub
        Intent inf = new Intent(MainActivity.this,information.class);
        Bundle bundle = new Bundle();
        bundle.putShort("TableName", status);
        bundle.putInt("_id", arg2+1);
        inf.putExtras(bundle);
        startActivity(inf);
      }
    });
  }
  
  public void TableShow(short i){
    Cursor cursor = dbHlp.QueryDB(i);
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this,android.R.layout.simple_list_item_2, cursor,new String [] { "_id", "name" },
    new int []{ android.R.id.text1, android.R.id.text2},0);
    listView.setAdapter(adapter);
  }
  public void onDestroy(){
    dbHlp.close();
  }  
}


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
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);
      
    }
  }
 
  @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,int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM "+TABLE_NAME[i]+" WHERE _id = '"+id+"'", null);
  return cursor;
  }
 
}


information.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
package com.example.whattoeatnow;
 
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RatingBar;
 
public class information extends Activity {
  
  private ImageButton back_btn;
  private EditText Name_edt,Phone_edt,addr_edt,fav_edt;
  private RatingBar rating;
  private Button del_btn,modification_btn;
  private String TABLE_NAME[] = { "breakfast", "lunch", "dinner","midnightsnack" };                                                       
  
  WhatToEatNowDB dbHlp;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.information);
    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.back1_btn);
    del_btn= (Button)findViewById(R.id.del_btn);
    modification_btn =(Button)findViewById(R.id.modification_btn);
    Name_edt = (EditText)findViewById(R.id.Name_edt);
    Phone_edt= (EditText)findViewById(R.id.Phone_edt);
    addr_edt= (EditText)findViewById(R.id.addr_edt);
    fav_edt= (EditText)findViewById(R.id.fav_edt);
    rating =(RatingBar)findViewById(R.id.rating);
    
    dbHlp = new WhatToEatNowDB(this);
    
    Bundle bd = this.getIntent().getExtras();
    final short status = bd.getShort("status");
    final int _id = bd.getInt("_id");
    Log.v("id",String.valueOf(_id));
    Cursor cursor = dbHlp.QueryDB(status, _id);
          Name_edt.setText(cursor.getString(1));
    
    back_btn.setOnClickListener(new OnClickListener(){
      @Override
      public void onClick(View arg0) {
        finish();
      }
    });
    
    
  }
}
 


目前卡在執行information.java的Name_edt.setText(cursor.getString(1));會出錯,因為先試試看所以並沒有把其他資料也一併顯示出來,之後會寫成將所以欄位的資料都顯示在這個Activity上。

錯誤Log如下
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
10-05 22:42:02.746: W/dalvikvm(2733): threadid=1: thread exiting with uncaught exception (group=0xb2e8c648)
10-05 22:42:02.746: E/AndroidRuntime(2733): FATAL EXCEPTION: main
10-05 22:42:02.746: E/AndroidRuntime(2733): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.whattoeatnow/com.example.whattoeatnow.information}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
10-05 22:42:02.746: E/AndroidRuntime(2733):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at android.os.Looper.loop(Looper.java:137)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at android.app.ActivityThread.main(ActivityThread.java:5103)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at java.lang.reflect.Method.invokeNative(Native Method)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at java.lang.reflect.Method.invoke(Method.java:525)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at dalvik.system.NativeStart.main(Native Method)
10-05 22:42:02.746: E/AndroidRuntime(2733): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
10-05 22:42:02.746: E/AndroidRuntime(2733):   at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at com.example.whattoeatnow.information.findView(information.java:59)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at com.example.whattoeatnow.information.onCreate(information.java:30)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at android.app.Activity.performCreate(Activity.java:5133)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-05 22:42:02.746: E/AndroidRuntime(2733):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-05 22:42:02.746: E/AndroidRuntime(2733):   ... 11 more


麻煩各位幫我看看我有沒有哪邊沒有注意到,或是邏輯錯誤。


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
1558 請問關於SQLite取出資料並顯示的問題 罪惡感 10667 2013-10-06 10:57
1399 Re:請問關於SQLite取出資料並顯示的問題 jimwayne 388 2013-10-06 11:12
1465 Re:請問關於SQLite取出資料並顯示的問題 罪惡感 6 2013-10-06 14:39
» JWorld@TW »  Java ME、Google Android 平台與 JavaCard 討論區 » Android

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

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8