我是 android 新手,在我的应用程序中我需要创建数据库,当用户将一些东西写入 EditText 时,我需要将它保存到数据库中,但我实际上不知道我将如何去做。所以请有人帮帮我。我已经创建了保存数据的提交按钮,现在我只需要创建一个数据库。
在这里,我给出了一个名为“Invitation.java”的活动类,其中包含
package com.ggit.trip.caster;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Invitation extends AppBaseActivity {
protected static final String Tag = "Invitation Activity";
private Button submit2;
private Button exit2;
private EditText Edit1;
private EditText Edit2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.invitation);
registerBaseActivityReceiver();
submit2 = (Button) findViewById(R.id.sub2_button1);
exit2=(Button)findViewById(R.id.button2);
Edit1 = (EditText) findViewById(R.id.editText1);
Edit2 = (EditText) findViewById(R.id.editText2);
submit2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Log.d(Tag, "EditText : " + Edit1.getText().toString());
Log.d(Tag, "EditText : " + Edit2.getText().toString());
Intent intent = new Intent(getApplicationContext(),
Registration.class);
startActivity(intent);
}
});
exit2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
closeAllActivities();
}
});
}
public void onDestroy() {
super.onDestroy();
unRegisterBaseActivityReceiver();
}
}
现在这里名为“invitation.xml”的 Invitation.jave 类的 xml 布局包含
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/sea_01"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="E-mail"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:background="@drawable/rounded_edittext"
android:ems="10"
android:hint=" email address"
android:inputType="textEmailAddress" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/editText1"
android:text="Phone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/textView2"
android:background="@drawable/rounded_edittext"
android:ems="10"
android:hint=" phone number"
android:inputType="phone" />
<Button
android:id="@+id/sub2_button1"
android:layout_width="100dp"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/textView1"
android:layout_marginRight="27dp"
android:background="@drawable/gloss_nine_patch"
android:text="Send Invitation" />
<Button
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="45dp"
android:layout_alignLeft="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_marginLeft="32dp"
android:background="@drawable/gloss_nine_patch"
android:text="Exit" />
</RelativeLayout>
现在我需要将这两个 EditText 的数据保存到数据库中。请写一个适当的或完整的活动类,因为我是新人。谢谢你。
@Naser 的 DataHelper.java
package i.am.arnob;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DataHelper extends SQLiteOpenHelper{
//database version, current ver is 1.
public static final int DATABASE_VER=1;
//database Name or db name
public static final String DATABASE_NAME="dataPerson";
//table Name, table person
public static final String TABLE_PERSON="person";
//table fields name,fist name,email and domain
//public static final String KEY_NAME="name";
//public static final String KEY_FIRST_NAME="first_name";
public static final String KEY_EMAIL="email";
public static final String KEY_DOMAIN="domain";
public DataHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//creating string sqlTable for creating a table
String sqlTable = "create table " +TABLE_PERSON+ "("/* +KEY_NAME+ " text," +KEY_FIRST_NAME+ " text," */ +KEY_EMAIL+ " text," +KEY_DOMAIN+ " text);";
//db.execSQL() will execute string which we provide and will create a table with given table name and fields.
db.execSQL(sqlTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}