· 9 years ago · Oct 06, 2016, 10:00 AM
1DATABASE CLASS
2 package com.demo.feedbackapp.database;
3
4 import android.content.ContentValues;
5 import android.content.Context;
6 import android.database.Cursor;
7 import android.database.DatabaseErrorHandler;
8 import android.database.sqlite.SQLiteDatabase;
9 import android.database.sqlite.SQLiteOpenHelper;
10
11
12 public class DataBaseHelper extends SQLiteOpenHelper {
13
14 private static final String TAG = "DatabaseHelper";
15
16 // Database Version
17 private static final int DATABASE_VERSION = 1;
18
19 // Database Name
20 private static final String DATABASE_NAME = "db_feedback.sql";
21
22 // Table Names
23 private static final String TABLE_FEEDBACK = "tbl_feedback";
24
25
26 //tbl_feedback Table- Column Names
27
28 private static final String KEY_ID = "id";
29 private static final String KEY_NAME = "name";
30 private static final String KEY_EMAIL = "email";
31 private static final String KEY_CONTACT = "contact";
32 private static final String KEY_ADDRESS = "address";
33 private static final String KEY_SERVICE = "service";
34
35
36 // Table Create Statements
37 // User table create statement
38 private static final String CREATE_TABLE_FEEDBACK = "CREATE TABLE "
39 + TABLE_FEEDBACK + "(" + KEY_ID + " INTEGER PRIMARY KEY autoincrement," + KEY_NAME + " TEXT," + KEY_EMAIL + " TEXT,"
40 + KEY_CONTACT + " TEXT," + KEY_ADDRESS + " TEXT,"
41 + KEY_SERVICE + " TEXT" + ")";
42
43
44 public DataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
45 super(context, name, factory, version, errorHandler);
46 }
47
48
49 public DataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
50 super(context, name, factory, version);
51 }
52
53 public DataBaseHelper(Context context) {
54 super(context, DATABASE_NAME, null, DATABASE_VERSION);
55 SQLiteDatabase db = this.getWritableDatabase();
56
57
58 }
59
60
61 @Override
62 public void onCreate(SQLiteDatabase db) {
63 // creating required tables
64 db.execSQL(CREATE_TABLE_FEEDBACK);
65 }
66
67 @Override
68 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
69 // on upgrade drop older tables
70 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FEEDBACK);
71
72 // create new tables
73 onCreate(db);
74 }
75
76 //insert in to user
77
78 public boolean insertFeedback(String name, String emai, String contact, String address, String service) {
79
80 SQLiteDatabase db = this.getWritableDatabase();
81 ContentValues contentValues = new ContentValues();
82
83 contentValues.put(KEY_NAME, name);
84 contentValues.put(KEY_EMAIL, emai);
85 contentValues.put(KEY_CONTACT, contact);
86 contentValues.put(KEY_ADDRESS, address);
87 contentValues.put(KEY_SERVICE, service);
88
89
90 long result = db.insert(TABLE_FEEDBACK, null, contentValues);
91 if (result == -1) {
92 return false;
93 } else {
94 return true;
95 }
96 }
97
98 // Get Data from table
99
100 public Cursor getFeedbackData() {
101 SQLiteDatabase db = this.getWritableDatabase();
102
103 String qry = "select * from " + TABLE_FEEDBACK;
104
105 Cursor res = db.rawQuery(qry, null);
106 return res;
107 }
108
109
110 }
111
112
113
114 MainActivity.class
115
116
117
118 package com.demo.feedbackapp;
119
120 import android.content.Context;
121 import android.database.Cursor;
122 import android.os.Bundle;
123 import android.support.v7.app.AppCompatActivity;
124 import android.text.TextUtils;
125 import android.util.Log;
126 import android.view.View;
127 import android.view.inputmethod.InputMethodManager;
128 import android.widget.EditText;
129 import android.widget.RadioButton;
130 import android.widget.RadioGroup;
131 import android.widget.TextView;
132 import android.widget.Toast;
133
134 import com.demo.feedbackapp.database.DataBaseHelper;
135
136 import java.text.SimpleDateFormat;
137 import java.util.Calendar;
138 import java.util.regex.Matcher;
139 import java.util.regex.Pattern;
140
141 public class MainActivity extends AppCompatActivity {
142
143 private String TAG = "MainActivity";
144
145
146 private EditText etName;
147 private EditText etEmail;
148 private EditText etContact;
149 private EditText etAddress;
150
151 private TextView tvSubmit;
152 private TextView tvDate;
153 private TextView tvSrno;
154
155 private RadioGroup radioGroupService;
156 private RadioButton radioButton;
157
158 private String currentDate;
159 private int srNo;
160
161 // Database class object
162 DataBaseHelper db;
163
164 // for email validation
165 public Pattern pattern;
166 public Matcher matcher;
167 public static final String EMAIL_PATTERN =
168 "^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@"
169 + "[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$";
170
171
172 @Override
173 protected void onCreate(Bundle savedInstanceState) {
174 super.onCreate(savedInstanceState);
175 setContentView(R.layout.activity_main);
176
177
178 etName = (EditText) findViewById(R.id.et_name);
179 etEmail = (EditText) findViewById(R.id.et_email);
180 etContact = (EditText) findViewById(R.id.et_contactno);
181 etAddress = (EditText) findViewById(R.id.et_address);
182
183 tvSubmit = (TextView) findViewById(R.id.tv_submit);
184 tvDate = (TextView) findViewById(R.id.tv_date_label);
185 tvSrno = (TextView) findViewById(R.id.tv_srno_label);
186
187 radioGroupService = (RadioGroup) findViewById(R.id.rdo_grp_service);
188
189 db = new DataBaseHelper(MainActivity.this);
190
191 currentDate = getCurrentDate();
192
193 tvDate.setText("Date : " + currentDate);
194
195 Cursor cursor = db.getFeedbackData();
196 if (cursor != null) {
197 int count = cursor.getCount();
198 srNo = count + 1;
199
200 Log.e(TAG, "Serial Number==>" + srNo);
201 tvSrno.setText("Sr no : " + srNo);
202 }
203
204
205 tvSubmit.setOnClickListener(new View.OnClickListener() {
206 @Override
207 public void onClick(View v) {
208
209
210 int selectedId = radioGroupService.getCheckedRadioButtonId();
211
212 // find the radiobutton by returned id
213 radioButton = (RadioButton) findViewById(selectedId);
214
215 if (validateForm()) {
216 String name = etName.getText().toString();
217 String email = etEmail.getText().toString();
218 String contact = etContact.getText().toString();
219 String address = etAddress.getText().toString();
220 String service = radioButton.getText().toString();
221 //inserting data
222 boolean isInserted = db.insertFeedback(name, email, contact, address, service);
223
224//check the data will inserted or not
225 if (isInserted) {
226 Toast.makeText(MainActivity.this, "Feedback Saved.", Toast.LENGTH_SHORT).show();
227 etName.setText("");
228 etEmail.setText("");
229 etContact.setText("");
230 etAddress.setText("");
231
232 etName.requestFocus();
233 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
234 imm.showSoftInput(etName, InputMethodManager.SHOW_IMPLICIT);
235
236 Cursor cursor = db.getFeedbackData();
237 if (cursor != null) {
238 int count = cursor.getCount();
239 srNo = count + 1;
240
241 Log.e(TAG, "Serial Number==>" + srNo);
242 tvSrno.setText("Sr no : " + srNo);
243 }
244
245
246 }
247
248 }
249
250
251 }
252 });
253
254
255 }
256
257 private String getCurrentDate() {
258
259 Calendar c = Calendar.getInstance();
260 System.out.println("Current time => " + c.getTime());
261
262 // SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
263 SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
264 String formattedDate = df.format(c.getTime());
265
266 return formattedDate;
267 }
268
269 /**
270 * To check that email address is valid or not
271 *
272 * @return true/false according to the validations
273 */
274 private boolean isValidEmail(String email) {
275 pattern = Pattern.compile(EMAIL_PATTERN);
276 matcher = pattern.matcher(email);
277
278 if (matcher.matches()) {
279 return true;
280 } else {
281 return false;
282 }
283 }
284
285 /**
286 * Validating user details form
287 */
288 private boolean validateForm() {
289 String name = etName.getText().toString();
290 String email = etEmail.getText().toString();
291 String contact = etContact.getText().toString();
292 String address = etAddress.getText().toString();
293 String service = radioButton.getText().toString();
294
295 if (TextUtils.isEmpty(name)) {
296 etName.setError("Please enter full name");
297 etName.requestFocus();
298 return false;
299 } else {
300 etName.setError(null);
301 }
302
303 if (TextUtils.isEmpty(contact)) {
304 etContact.setError("Please enter Mobile Number");
305 return false;
306 } else {
307 etContact.setError(null);
308 }
309
310
311 if (!TextUtils.isEmpty(email)) {
312 if (!isValidEmail(email)) {
313 etEmail.setError("Please enter valid Email");
314 etEmail.requestFocus();
315 return false;
316 } else {
317 etEmail.setError(null);
318 }
319 }
320
321 return true;
322 }
323 }