-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main4Activity.java
84 lines (70 loc) · 3.23 KB
/
Main4Activity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.example.build.blockpuzzle;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Main4Activity extends AppCompatActivity {
private Button save_btn;
private EditText usr_edit_text;
static TextView timeView;
DBHelper mydb;
String username;
class ButtonHandler implements View.OnClickListener {
@Override
public void onClick(View v) {
String usrStr = usr_edit_text.getText().toString();
// Check that user typed a username
if(usrStr.length()==0){
Toast.makeText(getApplicationContext(),
"Please enter a username", Toast.LENGTH_SHORT).show();
usr_edit_text.requestFocus();
return;
}
//check if there is an existing user or not
Cursor cursor = mydb.getPlayer(usrStr);
if (cursor != null) {
if (cursor.moveToFirst()) {//there is a record
username = cursor.getString(cursor.getColumnIndex(DBHelper.USERNAME_COL));
String old_score_str = cursor.getString(cursor.getColumnIndex(DBHelper.SCORE_COL));
Toast.makeText(getApplicationContext(),
"Welcome back " + usrStr + ". Your previous score is "
+ old_score_str, Toast.LENGTH_SHORT).show();
} else {//there is no record
Toast.makeText(getApplicationContext(),
usrStr + " added", Toast.LENGTH_SHORT).show();
mydb.insertPlayer(usrStr);
username = usrStr;
}
cursor.close();
//update score
mydb.updatePlayer(username, MainActivity.score);
Toast.makeText(getApplicationContext(),
"Your score " + String.valueOf(MainActivity.textView)
+ " is saved." , Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Main4Activity.this, Main3Activity.class);
startActivity(intent);
}
}//onClick()
}//end of ButtonHandler
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main4);
//create DBHelper object
mydb = new DBHelper(this);
//create a handler object
ButtonHandler buttonHandler = new ButtonHandler();
//get the button and attach the handler to it
save_btn = (Button)findViewById(R.id.save_btn);
save_btn.setOnClickListener(buttonHandler);
timeView = (TextView) findViewById(R.id.timeView);
timeView.setText("TIME: " + String.valueOf(MainActivity.textView));
usr_edit_text = (EditText)findViewById(R.id.usr_edit_text);
usr_edit_text.requestFocus();
}//end of onCreate
}//end of Activity4