-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.java
169 lines (136 loc) · 5.81 KB
/
MainActivity.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.example.weighttracker;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText date,weight,id;
Button insert_button,viewall_button,update_button,delete_button,avg_button,go_to_graph;
public static DecimalFormat df = new DecimalFormat("0.00");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
date = (EditText) findViewById(R.id.date_id);
id = (EditText) findViewById(R.id.id_delete_id);
weight = (EditText) findViewById(R.id.weight_id);
insert_button = (Button) findViewById(R.id.insert_id);
viewall_button = (Button) findViewById(R.id.viewall_id);
update_button = (Button)findViewById(R.id.update_button_id);
delete_button = (Button)findViewById(R.id.delete_button);
avg_button = (Button)findViewById(R.id.average_button);
go_to_graph = (Button)findViewById(R.id.graph_button);
AddData();
ViewAll();
ChangeData();
DeleteRow();
AverageData();
secondActivity();
}
public void ChangeData(){
update_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean done = myDb.UpdateData(id.getText().toString(),date.getText().toString(),weight.getText().toString());
if(done){
Toast.makeText(MainActivity.this,"Data Updated Successfully",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"Data Has Not been Updated",Toast.LENGTH_LONG).show();
}
}
});
}
public void DeleteRow(){
delete_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int done = myDb.deleteData(id.getText().toString());
if(done==0){
Toast.makeText(MainActivity.this,"Data for Id: "+id.getText().toString()+"not deleted.",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"Data for Id: "+id.getText().toString()+" deleted successfully",Toast.LENGTH_LONG).show();
}
}
});
}
public void AddData(){
insert_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean done = myDb.insertdata(date.getText().toString(),weight.getText().toString());
if(done==true){
Toast.makeText(MainActivity.this,"Data Inserted Successfully",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"Data Was Not Inserted",Toast.LENGTH_LONG).show();
}
}
});
}
public void AverageData(){
avg_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cr = myDb.getallData();
if(cr.getCount()==0){
showMessage("Average Weight: ","Nothing Has Been Inserted Yet!");
return;
}else{
double total=0.00;
int count = 0;
while(cr.moveToNext()){
total+=cr.getDouble(2);
count++;
}
double result = total/(double)count;
//String s = Double.toString(result);
showMessage("Average Weight: ", String.format("Your Average weight from " + count + " records is: " + df.format(result) + "kg(s) and the total is: " + df.format(total)+"kg(s)") );
}
}
});
}
public void ViewAll(){
viewall_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cr = myDb.getallData();
if(cr.getCount()==0){
//showMessage
showMessage("Your Records: ","Nothing Has Been Inserted Yet!");
return;
}else{
StringBuffer sb = new StringBuffer();
while(cr.moveToNext()){
sb.append("ID: "+ cr.getString(0)+"\n");
sb.append("DATE: "+ cr.getString(1)+"\n");
sb.append("WEIGHT: "+ cr.getString(2)+"\n\n");
}
showMessage("Your Records:",sb.toString());
}
}
});
}
public void showMessage(String title,String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void secondActivity(){
go_to_graph.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
}
}