· 9 years ago · Nov 11, 2016, 03:58 PM
1public class DatabaseUsername extends SQLiteOpenHelper {
2private static final String DATABASE_NAME="usernm.db";
3private static final int SCHEMA_VERSION=1;
4
5
6public DatabaseUsername(Context context) {
7 super(context, DATABASE_NAME, null, SCHEMA_VERSION);
8}
9
10@Override
11public void onCreate(SQLiteDatabase db) {
12 db.execSQL("CREATE TABLE almag (_id INTEGER PRIMARY KEY AUTOINCREMENT, nama TEXT, jekel TEXT);");
13 db.execSQL("CREATE TABLE score (_id INTEGER PRIMARY KEY AUTOINCREMENT, score INTEGER, userId INTEGER NOT NULL, FOREIGN KEY (userId) REFERENCES almag(_id) ON DELETE CASCADE);"); //create table score
14}
15
16@Override
17public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
18 // no-op, since will not be called until 2nd schema
19 // version exists
20
21}
22
23public Cursor getAll() {
24 return(getReadableDatabase()
25 .rawQuery("SELECT _id, nama, jekel FROM almag ORDER BY nama",
26 null));
27}
28
29public Cursor getById(String id) {
30 String[] args={id};
31
32 return(getReadableDatabase()
33 .rawQuery("SELECT _id FROM almag WHERE _ID=?",
34 args));
35
36}
37
38public void insert(String nama, String jekel) {
39 ContentValues cv=new ContentValues();
40
41 cv.put("nama", nama);
42 cv.put("jekel", jekel);
43
44 getWritableDatabase().insert("almag", nama, cv);
45}
46
47public void insertScore (int score, int userId) {
48 ContentValues cv=new ContentValues();
49
50 cv.put("score", score);
51 cv.put("userId", userId);
52 getWritableDatabase().insert("score", null, cv);
53}
54
55
56public String getNama(Cursor c) {
57 return(c.getString(1));
58}
59
60public String getJekel(Cursor c) {
61 return(c.getString(2));
62}
63
64public static int getId (Cursor c) {
65 return(c.getInt(1));
66}}
67
68public class UsernameList extends ListActivity {
69public final static String ID_EXTRA="com.rika.fyp.player";
70Cursor model=null;
71Cursor mode=null;
72AlmagAdapter adapter=null;
73EditText nama=null;
74RadioGroup jekel=null;
75DatabaseUsername helper=null;
76
77@Override
78public void onCreate(Bundle savedInstanceState) {
79 super.onCreate(savedInstanceState);
80 setContentView(R.layout.usernamelist);
81
82 helper=new DatabaseUsername(this);
83
84 nama=(EditText)findViewById(R.id.nama);
85 jekel=(RadioGroup)findViewById(R.id.jekel);
86
87 model=helper.getAll();
88 startManagingCursor(model);
89 adapter=new AlmagAdapter(model);
90 setListAdapter(adapter);
91}
92
93@Override
94public void onDestroy() {
95 super.onDestroy();
96
97 helper.close();
98}
99
100
101 public void onListItemClick(ListView parent, View view, int position, long id) {
102 Intent intent = new Intent(this, TheEndActivity.class);
103 Cursor cursor = (Cursor) adapter.getItem(position);
104 intent.putExtra("USER_ID", cursor.getInt(cursor.getColumnIndex("_id")));
105 startActivity(intent);
106
107 Intent i=new Intent(UsernameList.this, QuizAppActivity.class);
108 startActivity(i);
109 }
110
111
112@Override
113public boolean onCreateOptionsMenu(Menu menu) {
114 new MenuInflater(this).inflate(R.menu.option, menu);
115
116 return(super.onCreateOptionsMenu(menu));
117}
118
119@Override
120public boolean onOptionsItemSelected(MenuItem item) {
121 if (item.getItemId()==R.id.add) {
122 startActivity(new Intent(UsernameList.this, UsernameRegister.class));
123
124 return(true);
125 }
126
127 return(super.onOptionsItemSelected(item));
128}
129
130private View.OnClickListener onSave=new View.OnClickListener() {
131 public void onClick(View v) {
132 String type=null;
133
134 switch (jekel.getCheckedRadioButtonId()) {
135 case R.id.pria:
136 type="Pria";
137 break;
138 case R.id.perempuan:
139 type="Perempuan";
140 break;
141
142 }
143
144 helper.insert(nama.getText().toString(), type);
145
146 model.requery();
147 }
148};
149
150class AlmagAdapter extends CursorAdapter {
151 AlmagAdapter(Cursor c) {
152 super(UsernameList.this, c);
153 }
154
155 @Override
156 public void bindView(View row, Context ctxt,Cursor c) {
157 AlmagHolder holder=(AlmagHolder)row.getTag();
158
159 holder.populateFrom(c, helper);
160 }
161
162 @Override
163 public View newView(Context ctxt, Cursor c, ViewGroup parent) {
164 LayoutInflater inflater=getLayoutInflater();
165 View row=inflater.inflate(R.layout.usernamerow, parent, false);
166 AlmagHolder holder=new AlmagHolder(row);
167
168 row.setTag(holder);
169
170 return(row);
171 }
172}
173
174static class AlmagHolder {
175 private TextView nama=null;
176 private TextView alamat=null;
177 private ImageView icon=null;
178 private View row=null;
179
180 AlmagHolder(View row) {
181 this.row=row;
182
183 nama=(TextView)row.findViewById(R.id.title);
184 icon=(ImageView)row.findViewById(R.id.icon);
185 }
186
187 void populateFrom(Cursor c, DatabaseUsername helper) {
188 nama.setText(helper.getNama(c));
189
190 if (helper.getJekel(c).equals("Pria")) {
191 icon.setImageResource(R.drawable.pria);
192 }
193 else if (helper.getJekel(c).equals("Perempuan")) {
194 icon.setImageResource(R.drawable.perempuan);
195 }
196
197 }
198}}
199
200public class TheEndActivity extends Activity implements OnClickListener {
201
202 protected int userId;
203
204DatabaseUsername helper=null;
205Object almagId=null;
206int id;
207@Override
208public void onCreate(Bundle savedInstanceState) {
209 super.onCreate(savedInstanceState);
210 setContentView(R.layout.theendactivitylayout);
211 final SetGame currentGame = ((TheApplication)getApplication()).getCurrentGame();
212 String result = "Your Score is " + currentGame.getRight() + "/" + currentGame.getNumRounds() + ".. ";
213 final String comment = Mark.getResultComment(currentGame.getRight(), currentGame.getNumRounds(), getDifficultySettings());
214
215 userId = getIntent().getIntExtra("USER_ID", 0);
216 SQLiteDatabase db = (new DatabaseUsername(this)).getWritableDatabase();
217 final Cursor cursor = db.rawQuery("SELECT alm._id, alm.nama FROM almag alm WHERE emp._id = ?",
218 new String[]{""+userId});
219
220
221 TextView results = (TextView)findViewById(R.id.endgameResult);
222 results.setText(result + comment);
223
224 int image = Mark.getResultImage(currentGame.getRight(), currentGame.getNumRounds(), getDifficultySettings());
225 ImageView resultImage = (ImageView)findViewById(R.id.resultPage);
226 resultImage.setImageResource(image);
227
228 //handle button actions
229 Button finishBtn = (Button) findViewById(R.id.finishBtn);
230 Button answerBtn = (Button) findViewById(R.id.answerBtn);
231
232 finishBtn.setOnClickListener(new View.OnClickListener() {
233
234 @Override
235 public void onClick(View v) {
236
237 finish();
238
239 helper.insertScore(currentGame.getRight(), userId);
240
241 Intent i = new Intent(TheEndActivity.this, MainMenu.class);
242 startActivity(i);
243 }
244 });
245
246 answerBtn.setOnClickListener(new View.OnClickListener() {
247
248 @Override
249 public void onClick(View v) {
250
251 Intent i = new Intent(TheEndActivity.this, AnsActivity.class);
252 startActivityForResult(i, AppRule.PLAYBUTTON);
253
254 }
255 });
256
257}
258
259private int getDifficultySettings() {
260 SharedPreferences settings = getSharedPreferences(AppRule.SETTINGS, 0);
261 int diff = settings.getInt(AppRule.DIFFICULTY, 2);
262 return diff;
263}
264
265
266 public boolean onKeyDown(int keyCode, KeyEvent event)
267{
268 switch (keyCode)
269 {
270 case KeyEvent.KEYCODE_BACK :
271 return true;
272 }
273
274 return super.onKeyDown(keyCode, event);
275}
276
277
278 @Override
279 public void onClick(View arg0) {
280 // TODO Auto-generated method stub
281
282 }}
283
284finishBtn.setOnClickListener(new View.OnClickListener() {
285 @Override
286 public void onClick(View v) {
287
288 helper.insertMark(currentGame.getRight(), userId );
289 Intent i = new Intent(TheEndActivity.this, MainMenu.class);
290 startActivity(i);
291 }
292 });
293
294public void insertMark(int score, int userId) {
295 ContentValues cv=new ContentValues();
296
297 cv.put("score", score);
298 cv.put("userId", userId);
299
300 getWritableDatabase().insert("score", null, cv);
301}
302
303public void onListItemClick(ListView parent, View view, int position, long id) {
304 Intent intent = new Intent(this, TheEndActivity.class);
305 Cursor cursor = (Cursor) adapter.getItem((int)id);
306 intent.putExtra("USER_ID", cursor.getInt(cursor.getColumnIndex("_id")));
307 startActivity(intent);
308
309 Intent i=new Intent(UsernameList.this, QuizAppActivity.class);
310 startActivity(i);
311 }
312
313public void onListItemClick(ListView parent, View view, int position, long id) {
314 Intent intent = new Intent(this, TheEndActivity.class);
315
316 intent.putExtra("USER_ID", (int)id);
317 startActivity(intent);
318
319 Intent i=new Intent(UsernameList.this, QuizAppActivity.class);
320 startActivity(i);
321}