綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] 新新人求助, SQLite的問題

[複製連結] 查看: 1015|回覆: 1|好評: 0
跳轉到指定樓層
樓主
JiranWang | 收聽TA | 只看該作者 回帖獎勵 |正序瀏覽 |閱讀模式
發表於 2015-3-23 16:34

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
大家好:
今天第一次註冊, 對這裡不太熟, 但是又有問題想要問,
我正在學ANDROID 的APP, 在要資料存到手機上時,
在網路上找了一些資料, 來試著寫, 編譯沒問題, 但是在執行時,點進去就錯誤
請各位前輩, 如果有空的話, 能幫幫忙, 謝謝大家.
程式碼如下,
DBHelper.java
package com.ezit.weekapp.app;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DBHelper extends SQLiteOpenHelper {
    private static final String DB_NAME="weekapp.db";
    private static final String TBL_NAME="weekappset";
    private static final String CREATE_TBL=" create table " + TBL_NAME + " ( _id integer primary key autoincrement,week_set_1 text,week_set_2 text ) ";
    private final static int DATABASE_VERSION = 1;

    public DBHelper(Context context) {
        super(context, DB_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TBL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //final String DROP_TABLE = "DROP TABLE IF EXISTS " + TBL_NAME;
        //db.execSQL(DROP_TABLE);
        //onCreate(db);
    }
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
    }

}

Week2Activity.java
package com.ezit.weekapp.app;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

/**
* Created by 123 on 2015/3/11.
*/
public class Week2Activity extends Activity {
    private String keyword,page,lu,lu1,lu2;
    private TextView tv3;
    private Button btn1;
    private EditText et1;
    private Spinner sp1,sp2;
    private String[] lunch1 = {"預設(22)","16","18","20","22","24","26","28","30","32","34","36","38","40"};
    private String[] lunch2 = {"預設(白底)","黑底", "白底"};
    private ArrayAdapter<String> lunchList1,lunchList2;
    private DBHelper helper = null;
    private static final String TBL_NAME="weekappset";
    private Integer id=0,i;
    SQLiteDatabase db;

    protected void onCreate(Bundle savedInstanceState) {
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.week2);

        btn1 = (Button) findViewById(R.id.myButton01);
        tv3 = (TextView) findViewById(R.id.myTextView03);

        Intent intent = getIntent();
        Bundle bu = intent.getExtras();
        if (bu != null) {
            keyword = bu.getString("keyword");
            page = bu.getString("page");
            lu = bu.getString("lu");
        } else {
            keyword = "";
            page = "1";
            lu = "";
        }

        sp1 = (Spinner) findViewById(R.id.mySpinner01);
        lunchList1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lunch1);
        sp1.setAdapter(lunchList1);

        sp2 = (Spinner) findViewById(R.id.mySpinner02);
        lunchList2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lunch2);
        sp2.setAdapter(lunchList2);


        helper=new DBHelper(this);
        db = helper.getWritableDatabase();
        Cursor c = null;

        String[] ff={"_id","week_set_1","week_set_2"};
        c = db.query(TBL_NAME, ff, null, null, null, null,null,null);
        //tv3.setText(c.getCount());
        for(i=0;i<c.getCount();i++) {
            id = c.getInt(0);
            lu1 = c.getString(1);
            lu2 = c.getString(2);
            c.moveToNext();
        }
        c.close(); //關閉Cursor

        int spinnerPosition1 = lunchList1.getPosition(lu1);
        sp1.setSelection(spinnerPosition1);
        int spinnerPosition2 = lunchList2.getPosition(lu2);
        sp2.setSelection(spinnerPosition2);

        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Bundle bu = new Bundle();
                bu.putString("keyword", keyword.toString());
                bu.putString("page", page);
                bu.putString("lu", lu);
                Intent intent = new Intent(Week2Activity.this, MainActivity.class);
                intent.putExtras(bu);
                startActivity(intent);
            }
        });

        sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView adapterView, View view, int position, long id) {
                lu1 = sp1.getSelectedItem().toString();
                lu2 = sp2.getSelectedItem().toString();

                if (id == 0) {
                    add();
                } else {
                    update();
                }

                //tv3.setText(lu1+"/"+lu2);
            }

            @Override
            public void onNothingSelected(AdapterView arg0) {
            }

        });

        sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView adapterView, View view, int position, long id) {
                lu1 = sp1.getSelectedItem().toString();
                lu2 = sp2.getSelectedItem().toString();

                if (id == 0) {
                    add();
                } else {
                    update();
                }

                //tv3.setText(lu1+"/"+lu2);

            }

            @Override
            public void onNothingSelected(AdapterView arg0) {
            }

        });
    }

    private void update() {
        ContentValues values = new ContentValues();
        values.put("week_set_1", lu1);
        values.put("weel_set_2", lu2);
        db.update(TBL_NAME, values, "_id=" + id, null);
    }

    private void add() {
        ContentValues values = new ContentValues();
        values.put("week_set_1", lu1);
        values.put("week_set_2", lu2);
        db.insert(TBL_NAME, null, values);
    }

    private void del() {
        db.delete(TBL_NAME, null, null);
    }
}
「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
 樓主| JiranWang | 收聽TA | 只看該作者
發表於 2015-3-25 19:17
已經成功了, 謝謝大家了
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則