Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 518ab44

Browse files
author
Javatechig
committed
formatted code
1 parent 910d453 commit 518ab44

File tree

6 files changed

+123
-105
lines changed

6 files changed

+123
-105
lines changed

‎TODO Android App/src/com/javatechig/todo/AddTodoActivity.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import android.widget.EditText;
1010

1111
public class AddTodoActivity extends Activity implements OnClickListener {
12-
privateButtonaddTodoBtn;
13-
private SQLControllerdbController;
12+
13+
private ButtonaddTodoBtn;
1414
private EditText subjectEditText;
1515
private EditText descEditText;
16+
17+
private DBManager dbManager;
1618

1719
@Override
1820
protected void onCreate(Bundle savedInstanceState) {
@@ -21,30 +23,32 @@ protected void onCreate(Bundle savedInstanceState) {
2123
setTitle("Add Record");
2224

2325
setContentView(R.layout.activity_add_record);
26+
2427
subjectEditText = (EditText) findViewById(R.id.subject_edittext);
2528
descEditText = (EditText) findViewById(R.id.description_edittext);
2629

2730
addTodoBtn = (Button) findViewById(R.id.add_record);
2831

29-
dbController = new SQLController(this);
30-
dbController.open();
32+
dbManager = new DBManager(this);
33+
dbManager.open();
3134
addTodoBtn.setOnClickListener(this);
3235
}
3336

3437
@Override
3538
public void onClick(View v) {
3639
switch (v.getId()) {
3740
case R.id.add_record:
41+
3842
final String name = subjectEditText.getText().toString();
3943
final String desc = descEditText.getText().toString();
40-
dbController.insert(name, desc);
44+
45+
dbManager.insert(name, desc);
4146

4247
Intent main = new Intent(AddTodoActivity.this, TodoListActivity.class)
4348
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
49+
4450
startActivity(main);
4551
break;
46-
default:
47-
break;
4852
}
4953
}
5054

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.javatechig.todo;
2+
3+
import android.content.ContentValues;
4+
import android.content.Context;
5+
import android.database.Cursor;
6+
import android.database.SQLException;
7+
import android.database.sqlite.SQLiteDatabase;
8+
9+
public class DBManager {
10+
11+
private DatabaseHelper dbHelper;
12+
13+
private Context context;
14+
15+
private SQLiteDatabase database;
16+
17+
public DBManager(Context c) {
18+
context = c;
19+
}
20+
21+
public DBManager open() throws SQLException {
22+
dbHelper = new DatabaseHelper(context);
23+
database = dbHelper.getWritableDatabase();
24+
return this;
25+
}
26+
27+
public void close() {
28+
dbHelper.close();
29+
}
30+
31+
public void insert(String name, String desc) {
32+
ContentValues contentValue = new ContentValues();
33+
contentValue.put(DatabaseHelper.TODO_SUBJECT, name);
34+
contentValue.put(DatabaseHelper.TODO_DESC, desc);
35+
database.insert(DatabaseHelper.TABLE_NAME, null, contentValue);
36+
}
37+
38+
public Cursor fetch() {
39+
String[] columns = new String[] { DatabaseHelper._ID, DatabaseHelper.TODO_SUBJECT, DatabaseHelper.TODO_DESC };
40+
Cursor cursor = database.query(DatabaseHelper.TABLE_NAME, columns, null, null, null, null, null);
41+
if (cursor != null) {
42+
cursor.moveToFirst();
43+
}
44+
return cursor;
45+
}
46+
47+
public int update(long _id, String name, String desc) {
48+
ContentValues contentValues = new ContentValues();
49+
contentValues.put(DatabaseHelper.TODO_SUBJECT, name);
50+
contentValues.put(DatabaseHelper.TODO_DESC, desc);
51+
int i = database.update(DatabaseHelper.TABLE_NAME, contentValues, DatabaseHelper._ID + " = " + _id, null);
52+
return i;
53+
}
54+
55+
public void delete(long _id) {
56+
database.delete(DatabaseHelper.TABLE_NAME, DatabaseHelper._ID + "=" + _id, null);
57+
}
58+
59+
}

‎TODO Android App/src/com/javatechig/todo/DBhelper.java renamed to ‎TODO Android App/src/com/javatechig/todo/DatabaseHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import android.database.sqlite.SQLiteDatabase;
55
import android.database.sqlite.SQLiteOpenHelper;
66

7-
public class DBhelper extends SQLiteOpenHelper {
7+
public class DatabaseHelper extends SQLiteOpenHelper {
88

99
// Table Name
1010
public static final String TABLE_NAME = "TODOS";
@@ -24,7 +24,7 @@ public class DBhelper extends SQLiteOpenHelper {
2424
private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(" + _ID
2525
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TODO_SUBJECT + " TEXT NOT NULL, " + TODO_DESC + " TEXT);";
2626

27-
public DBhelper(Context context) {
27+
public DatabaseHelper(Context context) {
2828
super(context, DB_NAME, null, DB_VERSION);
2929
}
3030

‎TODO Android App/src/com/javatechig/todo/ModifyTodoActivity.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
import android.widget.EditText;
1010

1111
public class ModifyTodoActivity extends Activity implements OnClickListener {
12+
1213
private EditText titleText;
1314
private Button updateBtn, deleteBtn;
14-
private long _id;
15-
private SQLController dbController;
1615
private EditText descText;
16+
17+
private long _id;
18+
19+
private DBManager dbManager;
1720

1821
@Override
1922
protected void onCreate(Bundle savedInstanceState) {
@@ -23,8 +26,8 @@ protected void onCreate(Bundle savedInstanceState) {
2326

2427
setContentView(R.layout.activity_modify_record);
2528

26-
dbController = new SQLController(this);
27-
dbController.open();
29+
dbManager = new DBManager(this);
30+
dbManager.open();
2831

2932
titleText = (EditText) findViewById(R.id.subject_edittext);
3033
descText = (EditText) findViewById(R.id.description_edittext);
@@ -53,12 +56,12 @@ public void onClick(View v) {
5356
String title = titleText.getText().toString();
5457
String desc = descText.getText().toString();
5558

56-
dbController.update(_id, title, desc);
59+
dbManager.update(_id, title, desc);
5760
this.returnHome();
5861
break;
5962

6063
case R.id.btn_delete:
61-
dbController.delete(_id);
64+
dbManager.delete(_id);
6265
this.returnHome();
6366
break;
6467
}

‎TODO Android App/src/com/javatechig/todo/SQLController.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

‎TODO Android App/src/com/javatechig/todo/TodoListActivity.java

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,53 @@
2626
import android.widget.TextView;
2727

2828
public class TodoListActivity extends ActionBarActivity {
29-
private SQLController dbcon;
29+
30+
private DBManager dbManager;
31+
3032
private ListView listView;
31-
33+
34+
private SimpleCursorAdapter adapter;
35+
36+
final String[] from = new String[] { DatabaseHelper._ID,
37+
DatabaseHelper.TODO_SUBJECT, DatabaseHelper.TODO_DESC };
38+
39+
final int[] to = new int[] { R.id.id, R.id.title, R.id.desc };
40+
3241
@Override
3342
protected void onCreate(Bundle savedInstanceState) {
3443
super.onCreate(savedInstanceState);
44+
3545
setContentView(R.layout.fragment_emp_list);
3646

37-
dbcon = new SQLController(this);
38-
dbcon.open();
47+
dbManager = new DBManager(this);
48+
dbManager.open();
49+
Cursor cursor = dbManager.fetch();
3950

4051
listView = (ListView) findViewById(R.id.list_view);
4152
listView.setEmptyView(findViewById(R.id.empty));
42-
// Attach The Data From DataBase Into ListView Using Crusor Adapter
4353

44-
Cursor cursor = dbcon.fetch();
45-
46-
String[] from = new String[] { DBhelper._ID, DBhelper.TODO_SUBJECT, DBhelper.TODO_DESC};
47-
int[] to = new int[] { R.id.id, R.id.title, R.id.desc };
48-
49-
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.activity_view_record, cursor, from, to);
50-
54+
adapter = new SimpleCursorAdapter(this, R.layout.activity_view_record, cursor, from, to, 0);
5155
adapter.notifyDataSetChanged();
56+
5257
listView.setAdapter(adapter);
5358

5459
// OnCLickListiner For List Items
5560
listView.setOnItemClickListener(new OnItemClickListener() {
5661
@Override
5762
public void onItemClick(AdapterView<?> parent, View view, int position, long viewId) {
58-
TextView id_tv = (TextView) view.findViewById(R.id.id);
59-
TextView title_tv = (TextView) view.findViewById(R.id.title);
60-
TextView desc_tv = (TextView) view.findViewById(R.id.desc);
63+
TextView idTextView = (TextView) view.findViewById(R.id.id);
64+
TextView titleTextView = (TextView) view.findViewById(R.id.title);
65+
TextView descTextView = (TextView) view.findViewById(R.id.desc);
6166

62-
String id = id_tv.getText().toString();
63-
String title = title_tv.getText().toString();
64-
String desc = desc_tv.getText().toString();
67+
String id = idTextView.getText().toString();
68+
String title = titleTextView.getText().toString();
69+
String desc = descTextView.getText().toString();
6570

6671
Intent modify_intent = new Intent(getApplicationContext(), ModifyTodoActivity.class);
6772
modify_intent.putExtra("title", title);
6873
modify_intent.putExtra("desc", desc);
6974
modify_intent.putExtra("id", id);
75+
7076
startActivity(modify_intent);
7177
}
7278
});
@@ -80,22 +86,29 @@ public boolean onCreateOptionsMenu(Menu menu) {
8086

8187
@Override
8288
public boolean onOptionsItemSelected(MenuItem item) {
89+
8390
int id = item.getItemId();
8491
if (id == R.id.add_record) {
92+
8593
Intent add_mem = new Intent(this, AddTodoActivity.class);
8694
startActivity(add_mem);
87-
return true;
88-
}
89-
else if (id == R.id.export_records) {
90-
Cursor cursor = dbcon.fetch();
95+
96+
} else if (id == R.id.export_records) {
9197

98+
Cursor cursor = dbManager.fetch();
9299
exportToExcel(cursor);
93-
94-
return true;
95100
}
96101
return super.onOptionsItemSelected(item);
97102
}
98103

104+
105+
/**
106+
* Exports the cursor value to an excel sheet.
107+
* Recommended to call this method in a separate thread,
108+
* especially if you have more number of threads.
109+
*
110+
* @param cursor
111+
*/
99112
private void exportToExcel(Cursor cursor) {
100113

101114
final String fileName = "TodoList.xls";
@@ -123,24 +136,21 @@ private void exportToExcel(Cursor cursor) {
123136

124137
//Excel sheet name. 0 represents first sheet
125138
WritableSheet sheet = workbook.createSheet("MyShoppingList", 0);
126-
127-
//Supply row and column index
128-
Label label0 = new Label(0, 0, "Subject");
129-
Label label1 = new Label(1, 0, "Description");
130139

131140
try {
132-
sheet.addCell(label0);
133-
sheet.addCell(label1);
141+
sheet.addCell(newLabel(0, 0, "Subject")); // column and row
142+
sheet.addCell(newLabel(1, 0, "Description"));
134143

135144
if (cursor.moveToFirst()) {
136145
do {
137-
String title = cursor.getString(cursor.getColumnIndex(DBhelper.TODO_SUBJECT));
138-
String desc = cursor.getString(cursor.getColumnIndex(DBhelper.TODO_DESC));
146+
String title = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TODO_SUBJECT));
147+
String desc = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TODO_DESC));
139148

140149
int i = cursor.getPosition() + 1;
141150

142151
sheet.addCell(new Label(0, i, title));
143152
sheet.addCell(new Label(1, i, desc));
153+
144154
} while (cursor.moveToNext());
145155
}
146156

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /