main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"/>
<EditText android:id="@+id/username" android:layout_width="130dip"
android:background="@android:drawable/editbox_background_normal"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password : "/>
<EditText android:id="@+id/password" android:layout_width="130dip"
android:background="@android:drawable/editbox_background_normal"
android:layout_height="wrap_content"
android:password="true"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="City : "/>
<EditText android:id="@+id/city"
android:layout_width="130dip"
android:background="@android:drawable/editbox_background_normal"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zip : "/>
<EditText android:id="@+id/zip" android:layout_width="130dip"
android:background="@android:drawable/editbox_background_normal"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Country : "/>
<EditText android:id="@+id/country" android:layout_width="130dip"
android:background="@android:drawable/editbox_background_normal"
android:layout_height="wrap_content"/>
</LinearLayout>
<Button android:id="@+id/submit"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@android:drawable/editbox_background_normal"
android:text="Submit"/>
</LinearLayout>
Databaseedittext.java
package com.DatabaseEdittext;
import android.app.Activity;
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 Databaseedittext extends Activity {
private SimpleDBAdapter mDbHelper;
EditText user,pass,city,country,zip;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDbHelper = new SimpleDBAdapter(this);
mDbHelper.Open();
user=(EditText)findViewById(R.id.username);
pass=(EditText)findViewById(R.id.password);
city=(EditText)findViewById(R.id.city);
country=(EditText)findViewById(R.id.country);
zip=(EditText)findViewById(R.id.zip);
Button submit=(Button)findViewById(R.id.submit);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String username=user.getText().toString().trim();
String password=pass.getText().toString().trim();
String cityname=city.getText().toString().trim();
String countryname=country.getText().toString().trim();
String zipcode=zip.getText().toString().trim();
int zip_code=Integer.parseInt(zipcode);
Log.v("log_tag", "username" +username);
Log.v("log_tag", "password" +password);
Log.v("log_tag", "city" +cityname);
Log.v("log_tag", "country" +countryname);
Log.v("log_tag", "zip" +zipcode);
mDbHelper.insertdata(username, password, cityname, countryname, zip_code);
}
});
}
}
SimpleDBAdapter.java
package com.DatabaseEdittext;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class SimpleDBAdapter {
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String CITY = "city";
public static final String COUNTRY = "country";
public static final String ZIP = "zip";
public static final String KEY_ROWID = "_id";
private static final String TAG = "SimpleDBAdapter";
private DataBaseHelper mDbhelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE = "create table submit(_id integer primary key autoincrement,"
+ "username text not null,password text not null,city text not null,"
+ "country text not null,zip integer not null)";
private static final String DATABASE_NAME = "info";
private static final String DATABASE_TABLE = "submit";
private static final int DATABESE_VERSION = 2;
private final Context mCtx;
private static class DataBaseHelper extends SQLiteOpenHelper {
DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABESE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS submit");
onCreate(db);
}
}
public SimpleDBAdapter(Context ctx) {
this.mCtx = ctx;
}
public SimpleDBAdapter Open() throws SQLException {
mDbhelper = new DataBaseHelper(mCtx);
mDb = mDbhelper.getWritableDatabase();
return this;
}
public void close() {
mDbhelper.close();
}
public long insertdata(String username, String password, String city,
String country, int zip) {
ContentValues initialvalues = new ContentValues();
initialvalues.put(USERNAME, username);
initialvalues.put(PASSWORD, password);
initialvalues.put(CITY, city);
initialvalues.put(COUNTRY, country);
initialvalues.put(ZIP, zip);
return mDb.insert(DATABASE_TABLE, null, initialvalues);
}
public boolean deleteNote(long rowId) {
return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor fetchAllNotes() {
return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, USERNAME,
PASSWORD, CITY, COUNTRY, ZIP }, null, null, null, null, null);
}
public Cursor fetchNote(long rowId) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, USERNAME,
PASSWORD, CITY, COUNTRY, ZIP }, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateNote(long rowId, String username, String password,
String city, String country, int zip) {
ContentValues args = new ContentValues();
args.put(USERNAME, username);
args.put(PASSWORD, password);
args.put(CITY, city);
args.put(COUNTRY, country);
args.put(ZIP, zip);
return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
Thanks for the program.
ReplyDelete