-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteActivity.java
78 lines (70 loc) · 3.04 KB
/
WriteActivity.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
package com.example.a42305169.cosc341lab4;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
import java.io.FileOutputStream;
public class WriteActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write);
Spinner spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.divisions, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
public void onSubmit(View view) {
String filename = getResources().getString(R.string.dataFile);
String fileContents = getData();
if (fileContents.equals("")) return; // dont continue if there was an error
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_APPEND);
outputStream.write(fileContents.getBytes());
outputStream.close();
finish();
} catch(Exception e) {
Toast.makeText(getApplicationContext(), getString(R.string.write_error), Toast.LENGTH_SHORT).show();
}
}
private String getData() {
String snum = ((EditText) findViewById(R.id.editText)).getText().toString();
if (snum.length() != 8) {
Toast.makeText(getApplicationContext(), getString(R.string.snum_error), Toast.LENGTH_SHORT).show();
return "";
}
String fname = ((EditText) findViewById(R.id.editText3)).getText().toString();
String lname = ((EditText) findViewById(R.id.editText2)).getText().toString();
if (lname.equals("") && fname.equals("")) {
Toast.makeText(getApplicationContext(), getString(R.string.name_error), Toast.LENGTH_SHORT).show();
return "";
}
if (fname.equals("")) {
Toast.makeText(getApplicationContext(), getString(R.string.fname_error), Toast.LENGTH_SHORT).show();
return "";
}
if (lname.equals("")) {
Toast.makeText(getApplicationContext(), getString(R.string.lname_error), Toast.LENGTH_SHORT).show();
return "";
}
String gender = "Other";
RadioGroup rg = findViewById(R.id.radioGroup);
switch (rg.getCheckedRadioButtonId()) {
case R.id.radioButton:
//male
gender = "Male";
break;
case R.id.radioButton2:
//female
gender = "Female";
}
String division = ((Spinner) findViewById(R.id.spinner)).getSelectedItem().toString();
return snum+","+lname+","+fname+","+gender+","+division+"\n";
}
}