如何在按钮点击事件上将EditText数据添加到数据库?

时间:2012-08-08 12:47:23

标签: java android database sqlite

我是Android编程新手。我在一个单独的java类中创建了一个数据库,我有一个活动,我想要一些数据,特别是从EditText到按钮点击时存储到数据库。我在代码中做错了什么?在OnClick事件中包含数据库代码后,它不会启动我的savecount活动。

当SaveClass活动开始时,它会冻结应用程序,eclipse会显示以下消息:" Source Not Found"可以选择编辑源查找路径。

Logcat在递归过程中显示以下内容:

08-08 18:06:37.313: D/dalvikvm(1676): threadid=1: still suspended after undo (sc=1 dc=1)
08-08 18:06:37.323: I/dalvikvm(1676): Wrote stack traces to '/data/anr/traces.txt'
08-08 18:06:37.793: I/Process(896): Sending signal. PID: 1676 SIG: 3
08-08 18:06:37.793: I/dalvikvm(1676): threadid=3: reacting to signal 3
08-08 18:06:37.823: D/dalvikvm(1676): threadid=1: still suspended after undo (sc=1 dc=1)
08-08 18:06:37.823: I/dalvikvm(1676): Wrote stack traces to '/data/anr/traces.txt'
08-08 18:06:38.042: W/ActivityManager(896): Launch timeout has expired, giving up wake lock!
08-08 18:06:38.175: W/ActivityManager(896): Activity idle timeout for ActivityRecord{b4bf2e18 com.simple.tally.counter/.SaveCount}

classdbOpenHelper

package com.simple.tally.counter;

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


public class classdbOpenHelper extends SQLiteOpenHelper {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_COUNTED = "countedno";
    public static final String KEY_DESC = "countdesc";
    public static final String KEY_DATE = "countdate";

    public static final String DATABASE_NAME= "countdb";
    public static final String DATABASE_TABLE = "countable";
    public static final int DATABASE_VERSION = 1;


    public classdbOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE" + DATABASE_TABLE + " (" +
        KEY_ROWID + " INTEGET PRIMARY KEY AUTOINCREMENT " +
        KEY_COUNTED + " INTEGER " +
        KEY_DESC + " TEXT NOT NULL " +
        KEY_DATE + " TEXT NOT NULL);"
                ); 
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXITS " + DATABASE_TABLE);
        onCreate(db);

    }

}

SaveCount.java

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

public class SaveCount extends Activity {
TextView metersave;
classdbOpenHelper eventsData;
EditText desc;
Button Save;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_save_count);        
        Intent launchNewIntent = getIntent();
        String count = launchNewIntent.getStringExtra(SimpleTallyCounter.EXTRA_MESSAGE);
        metersave = (TextView) findViewById(R.id.metersave);
        metersave.setText(count); 
        final Button Save = (Button) findViewById(R.id.Save);
        Save.setOnClickListener(new clicker());
        eventsData = new classdbOpenHelper(this);
        desc = (EditText) findViewById(R.id.desc);
    }

    @Override
    public void onDestroy() {
      eventsData.close();
    }

    class clicker implements Button.OnClickListener {
        public void onClick(View v) {
            String descvalue = desc.getText().toString();
            uSQLiteDatabase db = eventsData.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put(classdbOpenHelper.KEY_COUNTED, metersave.getText().toString());
            cv.put(classdbOpenHelper.KEY_DESC, descvalue);
            cv.put(classdbOpenHelper.KEY_DATE, System.currentTimeMillis());
            db.insert(classdbOpenHelper.DATABASE_TABLE, null, cv);
        }
    }


}

1 个答案:

答案 0 :(得分:0)

像这样createComment使用此功能将值存储在数据库

   ed1 = (EditText) findViewById(R.id.ed1);
   ed2 = (EditText) findViewById(R.id.ed2);
   ed3 = (EditText) findViewById(R.id.ed3);


  String str1 = ed1.getText().toString();
  String str2 = ed2.getText().toString();
  String str3 = ed3.getText().toString();
// stord data base values in database            
 comment = datasource.createComment(str1, str2 ,str3);

anr在另一项活动中恢复数据库值getAllAccounts

  AccountDataSource data = new AccountDataSource(getActivity());
    data.open();
    Accounts_list = data.getAllAccounts();

就像看http://www.vogella.com/articles/AndroidSQLite/article.html一样,它已经用完了。