/**********************MySQLiteHelper*************************/
package com.sampleapp.application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLiteHelper extends SQLiteOpenHelper {
MySQLiteHelper dbHelper;
SQLiteDatabase database;
public static class EmpTable{
static final String TABLE_NAME = "employee";
static final String EMP_ID = "_id";
static final String EMP_NAME = "name";
static final String EMP_DESIGNATION = "designation";
static final String EMP_COMPANY = "company";
static final String EMP_MOBILE = "mobile";
static final String EMP_ADDRESS = "address";
}
public static final String TABLE_COMMENTS = "comments";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_COMMENT = "comment";
public static final String DATABASE_NAME = "comments.db";
private static final int DATABASE_VERSION = 1;
private String[] allColumns = { COLUMN_ID, COLUMN_COMMENT };
// Database creation sql statement
public static final String DATABASE_CREATE = "create table "
+ TABLE_COMMENTS + "( " + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_COMMENT
+ " text not null);";
public static final String CREATE_EMP_TABLE = "create table "
+ EmpTable.TABLE_NAME + "( " + EmpTable.EMP_ID
+ " integer primary key autoincrement, "
+ EmpTable.EMP_NAME + " text not null, "
+ EmpTable.EMP_DESIGNATION + " text not null, "
+ EmpTable.EMP_COMPANY + " text not null, "
+ EmpTable.EMP_MOBILE + " real, "
+ EmpTable.EMP_ADDRESS + " text);";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
database.execSQL(CREATE_EMP_TABLE);
Log.d("\n\nMySQLiteHelper***", "Table Created");
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
database.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
database.execSQL("DROP TABLE IF EXISTS " + EmpTable.TABLE_NAME);
onCreate(database);
Log.d("\n\nMySQLiteHelper***", "Table Upgraded");
}
}
/*************************CommentsDataSource.java****************************/
package com.sampleapp.application;
import com.sampleapp.application.MySQLiteHelper.EmpTable;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class CommentsDataSource {
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
public CommentsDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
Log.d("/nDATABASE OPENED************", "OPENED");
}
public void close() {
dbHelper.close();
Log.d("/nDATABASE CLOSED************", "CLOSED");
}
public void createComment(String comment) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_COMMENT, comment);
database.insert(MySQLiteHelper.TABLE_COMMENTS, null, values);
Log.d("\nINSERT", "Inserted the data..\n");
}
public Cursor all(Activity activity) {
String[] from = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_COMMENT };
String order = MySQLiteHelper.COLUMN_ID;
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, from,
null, null, null, null, order);
activity.startManagingCursor(cursor);
return cursor;
}
public int count() {
return (int) DatabaseUtils.queryNumEntries(database,
MySQLiteHelper.TABLE_COMMENTS);
}
public void deleteComment(long id) {
System.out.println("Comment deleted with id: " + id);
database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID
+ " = " + id, null);
}
public void createEmployee(String name, String degn, String cmpy, int mob,
String address) {
ContentValues values = new ContentValues();
values.put("name", name);
values.put("designation", degn);
values.put("company", cmpy);
values.put("mobile", mob);
values.put("address", address);
try {
database.insertOrThrow(EmpTable.TABLE_NAME, null, values);
Log.v("\nINSERT IN Employee table", "Inserted a row..\n");
} catch (SQLException e) {
Log.e(MySQLiteHelper.DATABASE_NAME, e.toString());
}
}
public Cursor allEmployees(Activity activity) {
String[] from = { EmpTable.EMP_ID, EmpTable.EMP_NAME,
EmpTable.EMP_DESIGNATION, EmpTable.EMP_COMPANY,
EmpTable.EMP_MOBILE, EmpTable.EMP_ADDRESS };
String order = EmpTable.EMP_ID;
Cursor cursor = database.query(EmpTable.TABLE_NAME, from,
null, null, null, null, order);
activity.startManagingCursor(cursor);
return cursor;
}
/* public void createTable(){
database.execSQL(MySQLiteHelper.CREATE_EMP_TABLE);
}
public void dropTable(){
database.execSQL("DROP TABLE IF EXISTS " + EmpTable.TABLE_NAME);
}*/
}
/**************************LoginActivity.java***************************/
package com.sampleapp.application;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class LoginActivity extends Activity implements OnClickListener {
protected static final int LENGTH_SHORT = 0;
Button btn_view;
Button btn_count;
Button btn_insert;
Button btn_delete;
EditText new_data;
EditText del_data;
int count_comment;
Comment comment;
private CommentsDataSource datasource;
SimpleCursorAdapter adapter;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
datasource = new CommentsDataSource(this);
btn_view = (Button) findViewById(R.id.btn_view);
btn_view.setOnClickListener(this);
btn_insert = (Button) findViewById(R.id.btn_insert);
btn_insert.setOnClickListener(this);
btn_delete = (Button) findViewById(R.id.btn_delete);
btn_delete.setOnClickListener(this);
btn_count = (Button) findViewById(R.id.btn_count);
btn_count.setOnClickListener(this);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification_icon, "Hello......", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, LoginActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), "My notification", "Hello World!", contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_delete:
count_comment = (int) datasource.count();
del_data = (EditText) findViewById(R.id.del_data);
int delData = Integer.parseInt(del_data.getText().toString());
if (delData <= count_comment)
datasource.deleteComment((int) delData);
Log.d("\nDELETED PARTICULAR DATA ************", "NUMBER -> " + delData);
break;
case R.id.btn_count:
count_comment = (int) datasource.count();
System.out.println("\nCOUNT------------" + count_comment);
Toast.makeText(getApplicationContext(), "Total comments: "+count_comment,
Toast.LENGTH_LONG).show();
break;
case R.id.btn_insert:
new_data = (EditText) findViewById(R.id.new_data);
String newData = new_data.getText().toString();
datasource.createComment(newData);
break;
case R.id.btn_view:
String[] from = new String[] { MySQLiteHelper.COLUMN_ID , MySQLiteHelper.COLUMN_COMMENT };
int[] to = new int[] { R.id.col_id, R.id.label };
cursor = datasource.all(this);
adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor,
from, to);
ListView av = (ListView) findViewById(R.id.comment_list);
av.setAdapter(adapter);
av.setFastScrollEnabled(true);
av.setTextFilterEnabled(true);
av.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View v,
int clickedPos, long id) {
// dataAdapter.remove(dataAdapter.getItem(clickedpos));
// dataAdapter.insert(t.getText().toString(), clickedpos);
Toast.makeText(LoginActivity.this, "Pos: "+ clickedPos, Toast.LENGTH_SHORT);
Log.d("LONG CLICK---------", "clicked --> "+clickedPos);
return true;
}
});
break;
}
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
}
/********************Comment.java*********************/
package com.sampleapp.application;
public class Comment {
private long id;
private String comment;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
// Will be used by the ArrayAdapter in the ListView
@Override
public String toString() {
return comment;
}
}
/*********************register.xml***********************/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn_count"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/count" >
</Button>
<Button
android:id="@+id/btn_view"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_toRightOf="@+id/btn_count"
android:text="@string/view" >
</Button>
<EditText
android:id="@+id/new_data"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_count" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btn_insert"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_toRightOf="@+id/new_data"
android:text="@string/insert"
android:layout_below="@+id/btn_view">
</Button>
<EditText
android:id="@+id/del_data"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@+id/new_data">
</EditText>
<Button
android:id="@+id/btn_delete"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_toRightOf="@+id/del_data"
android:text="@string/delete"
android:layout_below="@+id/btn_insert">
</Button>
</RelativeLayout>
<ListView
android:id="@+id/comment_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
/*********************IMAGE VIEW**********************/
No comments:
Post a Comment