First Require to Create Layout Like Following.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtName"
android:hint="Name"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save Data"
android:id="@+id/btnSave"
android:layout_below="@+id/txtName"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lstName"
android:layout_below="@+id/btnSave"
android:layout_centerHorizontal="true" />
</RelativeLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtName"
android:hint="Name"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save Data"
android:id="@+id/btnSave"
android:layout_below="@+id/txtName"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lstName"
android:layout_below="@+id/btnSave"
android:layout_centerHorizontal="true" />
</RelativeLayout>
raw.xml / / file for row
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lblName"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
After th create Following Classes
DBHelper.java
package pack.com.contactdemo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper{
//table name
public static final String TABLE_NAME="contact";
//table column
public static final String _ID="_id";
public static final String NAME="name";
//database
public static final String DATABASE_NAME="CONTACTS.DB";
//version
public static final int version=1;
//table query
public static final String QUERY_TABLE="create table " + TABLE_NAME + "(" + _ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " TEXT NOT NULL);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(QUERY_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper{
//table name
public static final String TABLE_NAME="contact";
//table column
public static final String _ID="_id";
public static final String NAME="name";
//database
public static final String DATABASE_NAME="CONTACTS.DB";
//version
public static final int version=1;
//table query
public static final String QUERY_TABLE="create table " + TABLE_NAME + "(" + _ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " TEXT NOT NULL);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(QUERY_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
SQLController.java
package pack.com.contactdemo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.sql.SQLException;
public class SQLController {
public DBHelper dbHelper;
private Context ourcontext;
private SQLiteDatabase database;
public SQLController(Context c) {
ourcontext = c;
}
public SQLController open() throws SQLException {
dbHelper = new DBHelper(ourcontext);
database = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}
public void insert(String name) {
ContentValues contentValue = new ContentValues();
contentValue.put(DBHelper.NAME, name);
database.insert(DBHelper.TABLE_NAME,null,contentValue);
}
public Cursor fetch() {
String[] columns = new String[] { DBHelper._ID, DBHelper.NAME };
Cursor cursor = database.query(DBHelper.TABLE_NAME, columns, null,
null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
}
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.sql.SQLException;
public class SQLController {
public DBHelper dbHelper;
private Context ourcontext;
private SQLiteDatabase database;
public SQLController(Context c) {
ourcontext = c;
}
public SQLController open() throws SQLException {
dbHelper = new DBHelper(ourcontext);
database = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}
public void insert(String name) {
ContentValues contentValue = new ContentValues();
contentValue.put(DBHelper.NAME, name);
database.insert(DBHelper.TABLE_NAME,null,contentValue);
}
public Cursor fetch() {
String[] columns = new String[] { DBHelper._ID, DBHelper.NAME };
Cursor cursor = database.query(DBHelper.TABLE_NAME, columns, null,
null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
}
MyAdpter.java
package pack.com.contactdemo;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class MyAdpter extends BaseAdapter
{
Context context;
String name;
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return null;
}
}
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class MyAdpter extends BaseAdapter
{
Context context;
String name;
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return null;
}
}
MainActivity.java
package pack.com.contactdemo;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import java.sql.SQLException;
public class MainActivity extends Activity implements View.OnClickListener {
EditText txtName;
DBHelper dbHelper;
SQLController dbcon;
// Long num;
String name;
Button btnSave;
ListView lstName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
initializeListView();
btnSave.setOnClickListener(this);
}
public void initialize() {
txtName = (EditText) findViewById(R.id.txtName);
btnSave = (Button) findViewById(R.id.btnSave);
lstName = (ListView) findViewById(R.id.lstName);
dbHelper=new DBHelper(MainActivity.this);
dbcon=new SQLController(MainActivity.this);
try {
dbcon.open();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void initializeListView()
{
Cursor cursor = dbcon.fetch();
String[] from = new String[] { DBHelper.NAME};
int[] to = new int[] { R.id.lblName };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.raw, cursor, from,to);
adapter.notifyDataSetChanged();
lstName.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.newName) {
Toast.makeText(getBaseContext(),"You Selected New",Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
name=txtName.getText().toString();
Log.i("Name",name);
if (name==" ")
{
Toast.makeText(getBaseContext(), " Put Name Please", Toast.LENGTH_SHORT).show();
initializeListView();
}
else
{
dbcon.insert(name);
Toast.makeText(getBaseContext(), "Data Sucessfully Saved", Toast.LENGTH_SHORT).show();
initializeListView();
}
}
}
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import java.sql.SQLException;
public class MainActivity extends Activity implements View.OnClickListener {
EditText txtName;
DBHelper dbHelper;
SQLController dbcon;
// Long num;
String name;
Button btnSave;
ListView lstName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
initializeListView();
btnSave.setOnClickListener(this);
}
public void initialize() {
txtName = (EditText) findViewById(R.id.txtName);
btnSave = (Button) findViewById(R.id.btnSave);
lstName = (ListView) findViewById(R.id.lstName);
dbHelper=new DBHelper(MainActivity.this);
dbcon=new SQLController(MainActivity.this);
try {
dbcon.open();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void initializeListView()
{
Cursor cursor = dbcon.fetch();
String[] from = new String[] { DBHelper.NAME};
int[] to = new int[] { R.id.lblName };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.raw, cursor, from,to);
adapter.notifyDataSetChanged();
lstName.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.newName) {
Toast.makeText(getBaseContext(),"You Selected New",Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
name=txtName.getText().toString();
Log.i("Name",name);
if (name==" ")
{
Toast.makeText(getBaseContext(), " Put Name Please", Toast.LENGTH_SHORT).show();
initializeListView();
}
else
{
dbcon.insert(name);
Toast.makeText(getBaseContext(), "Data Sucessfully Saved", Toast.LENGTH_SHORT).show();
initializeListView();
}
}
}
No comments:
Post a Comment