This is simpl application which insert data into Sqlite database --and shows the data from the database in a ListView
This is the first Activity of application which shows data from database in listview. Register here buton will start a Registration Activity.
Submit button will add data to database and show it in the ListView of MainActivity. Update can be performed by clicking ListView items.
To create this Simpl application do the following:-
1. Create a class which extends SQLiteOpenHelper the class is given below
package com.arun.registrationform;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class RegistrationOpenHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "REGISTRATION_DB";
public static final String TABLE_NAME = "REGISTRATION_TABLE";
public static final int VERSION = 1;
public static final String KEY_ID = "_id";
public static final String FNAME = "F_NAME";
public static final String LNAME = "L_NAME";
public static final String SCRIPT = "create table " + TABLE_NAME + " ("
+ KEY_ID + " integer primary key autoincrement, " + FNAME
+ " text not null, " + LNAME + " text not null );";
public RegistrationOpenHelper(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
db.execSQL(SCRIPT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("drop table " + TABLE_NAME);
onCreate(db);
}
2.create another class for writing all the Sqlite functions the class is given below
package com.arun.registrationform;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class RegistrationAdapter {
SQLiteDatabase database_ob;
RegistrationOpenHelper openHelper_ob;
Context context;
public RegistrationAdapter(Context c) {
context = c;
}
public RegistrationAdapter opnToRead() {
openHelper_ob = new RegistrationOpenHelper(context,
openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
database_ob = openHelper_ob.getReadableDatabase();
return this;
}
public RegistrationAdapter opnToWrite() {
openHelper_ob = new RegistrationOpenHelper(context,
openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
database_ob = openHelper_ob.getWritableDatabase();
return this;
}
public void Close() {
database_ob.close();
}
public long insertDetails(String fname, String lname) {
ContentValues contentValues = new ContentValues();
contentValues.put(openHelper_ob.FNAME, fname);
contentValues.put(openHelper_ob.LNAME, lname);
opnToWrite();
long val = database_ob.insert(openHelper_ob.TABLE_NAME, null,
contentValues);
Close();
return val;
}
public Cursor queryName() {
String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.FNAME,
openHelper_ob.LNAME };
opnToWrite();
Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols, null,
null, null, null, null);
return c;
}
public Cursor queryAll(int nameId) {
String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.FNAME,
openHelper_ob.LNAME };
opnToWrite();
Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols,
openHelper_ob.KEY_ID + "=" + nameId, null, null, null, null);
return c;
}
public long updateldetail(int rowId, String fname, String lname) {
ContentValues contentValues = new ContentValues();
contentValues.put(openHelper_ob.FNAME, fname);
contentValues.put(openHelper_ob.LNAME, lname);
opnToWrite();
long val = database_ob.update(openHelper_ob.TABLE_NAME, contentValues,
openHelper_ob.KEY_ID + "=" + rowId, null);
Close();
return val;
}
public int deletOneRecord(int rowId) {
// TODO Auto-generated method stub
opnToWrite();
int val = database_ob.delete(openHelper_ob.TABLE_NAME,
openHelper_ob.KEY_ID + "=" + rowId, null);
Close();
return val;
}
}
?
3.Then creae MainActivty and main.xml which shows the values from database in a listView.
This is MainActivity.java
package com.arun.registrationform;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
RegistrationAdapter adapter_ob;
RegistrationOpenHelper helper_ob;
SQLiteDatabase db_ob;
ListView nameList;
Button registerBtn;
Cursor cursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nameList = (ListView) findViewById(R.id.lv_name);
registerBtn = (Button) findViewById(R.id.btn_register);
adapter_ob = new RegistrationAdapter(this);
String[] from = { helper_ob.FNAME, helper_ob.LNAME };
int[] to = { R.id.tv_fname, R.id.tv_lname };
cursor = adapter_ob.queryName();
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.row, cursor, from, to);
nameList.setAdapter(cursorAdapter);
nameList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Bundle passdata = new Bundle();
Cursor listCursor = (Cursor) arg0.getItemAtPosition(arg2);
int nameId = listCursor.getInt(listCursor
.getColumnIndex(helper_ob.KEY_ID));
// Toast.makeText(getApplicationContext(),
// Integer.toString(nameId), 500).show();
passdata.putInt("keyid", nameId);
Intent passIntent = new Intent(MainActivity.this,
EditActivity.class);
passIntent.putExtras(passdata);
startActivity(passIntent);
}
});
registerBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent registerIntent = new Intent(MainActivity.this,
RegistrationActivity.class);
startActivity(registerIntent);
}
});
}
@Override
public void onResume() {
super.onResume();
cursor.requery();
}
}
Main.xml is given below
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
-->> for listView we need another layout t ospecify each Row of the LIstView
which is row.xml given below
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:id="@+id/tv_fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:id="@+id/tv_lname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
4.To inserting data into database RegistrationActivity.java and register.xml is used which is given below
RegistrationActivity.java
package com.arun.registrationform;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class RegistrationActivity extends Activity {
RegistrationAdapter adapter;
RegistrationOpenHelper helper;
EditText fnameEdit, lnameEdit;
Button submitBtn, resetBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
fnameEdit = (EditText) findViewById(R.id.et_fname);
lnameEdit = (EditText) findViewById(R.id.et_lname);
submitBtn = (Button) findViewById(R.id.btn_submit);
resetBtn = (Button) findViewById(R.id.btn_reset);
adapter = new RegistrationAdapter(this);
submitBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String fnameValue = fnameEdit.getText().toString();
String lnameValue = lnameEdit.getText().toString();
long val = adapter.insertDetails(fnameValue, lnameValue);
// Toast.makeText(getApplicationContext(), Long.toString(val),
// 300).show();
finish();
}
});
resetBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
fnameEdit.setText("");
lnameEdit.setText("");
}
});
}
}
?
register.xml
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1" >
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fname" />
android:id="@+id/et_fname"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lname" />
android:id="@+id/et_lname"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
\
5.For updating Database values EditActivity.java is used
package com.arun.registrationform;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class EditActivity extends Activity {
RegistrationAdapter regadapter;
RegistrationOpenHelper openHelper;
int rowId;
Cursor c;
String fNameValue, lNameValue;
EditText fname, lname;
Button editSubmit, btnDelete;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editregister);
fname = (EditText) findViewById(R.id.et_editfname);
lname = (EditText) findViewById(R.id.et_editlname);
editSubmit = (Button) findViewById(R.id.btn_update);
btnDelete = (Button) findViewById(R.id.btn_delete);
Bundle showData = getIntent().getExtras();
rowId = showData.getInt("keyid");
// Toast.makeText(getApplicationContext(), Integer.toString(rowId),
// 500).show();
regadapter = new RegistrationAdapter(this);
c = regadapter.queryAll(rowId);
if (c.moveToFirst()) {
do {
fname.setText(c.getString(1));
lname.setText(c.getString(2));
} while (c.moveToNext());
}
editSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
regadapter.updateldetail(rowId, fname.getText().toString(),
lname.getText().toString());
finish();
}
});
btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
regadapter.deletOneRecord(rowId);
finish();
}
});
}
}
editregister.xml is given below
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_height="wrap_content"
android:stretchColumns="1" >
android:layout_height="wrap_content" >
android:layout_height="wrap_content"
android:text="@string/fname" />
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
android:layout_height="wrap_content" >
android:layout_height="wrap_content"
android:text="@string/fname" />
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:orientation="horizontal" >
No comments:
Post a Comment