Commit 7a2d67a24c20f3dd30e4af828164da057bf9e11c

Authored by Ali B
1 parent d7644466

Completed the SQLite database and now new evaluations can be added per activity …

…and is saved in the database, and for every activity a list of all evaluations saved in the database is displayed
Risiko/.idea/caches/build_file_checksums.ser
No preview for this file type
Risiko/app/src/main/java/activity/Activities.java
... ... @@ -15,7 +15,6 @@ import dataclasses.Activity;
15 15  
16 16 public class Activities extends AppCompatActivity {
17 17 ListView lv;
18   - String chosenActivity;
19 18  
20 19 @Override
21 20 protected void onCreate(Bundle savedInstanceState) {
... ... @@ -36,19 +35,19 @@ public class Activities extends AppCompatActivity {
36 35 private void insertActivities(List<Activity> activities) {
37 36 lv = (ListView) findViewById(R.id.lv_activities);
38 37 lv.setAdapter(new ListAdapterActivities(this, activities));
39   - lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
40   - @Override
41   - public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
42   - chosenActivity = ((Activity) lv.getItemAtPosition(position)).getNameOfActivity();
43   - goToChosenActivity();
44   - }
  38 + lv.setOnItemClickListener((parent, view, position, id) -> {
  39 + String nameOfChosenActivity = ((Activity) lv.getItemAtPosition(position)).getNameOfActivity();
  40 + int activityId = ((Activity) lv.getItemAtPosition(position)).getId();
  41 + System.out.println(activityId);
  42 + goToChosenActivity(nameOfChosenActivity, activityId);
45 43 });
46 44 }
47 45  
48 46  
49   - private void goToChosenActivity() {
  47 + private void goToChosenActivity(String nameOfChosenActivity, int activityId) {
50 48 Intent intent = new Intent(this, Evaluations.class);
51   - intent.putExtra("NAME_OF_CHOSEN_ACTIVITY", chosenActivity);
  49 + intent.putExtra("NAME_OF_CHOSEN_ACTIVITY", nameOfChosenActivity);
  50 + intent.putExtra("ID_OF_CHOSEN_ACTIVITY", activityId);
52 51 startActivity(intent);
53 52 }
54 53 }
... ...
Risiko/app/src/main/java/activity/Evaluations.java
... ... @@ -25,7 +25,7 @@ import dataclasses.Evaluation;
25 25 public class Evaluations extends AppCompatActivity {
26 26 ListView lv;
27 27 String chosenEvaluation;
28   - TextView consequence;
  28 + TextView tvNameOfActivity;
29 29 Button addNewEvaluation;
30 30 DatabaseSource dbs;
31 31  
... ... @@ -33,16 +33,22 @@ public class Evaluations extends AppCompatActivity {
33 33 protected void onCreate(Bundle savedInstanceState) {
34 34 super.onCreate(savedInstanceState);
35 35 setContentView(R.layout.activity_evaluations);
  36 +
  37 +
36 38 dbs = new DatabaseSource(this);
  39 + Intent intent = getIntent();
  40 + String activityName = intent.getStringExtra("NAME_OF_CHOSEN_ACTIVITY");
  41 + int activityId = intent.getIntExtra("ID_OF_CHOSEN_ACTIVITY", -1);
  42 +
  43 + tvNameOfActivity = findViewById(R.id.tv_nameOfActivity);
  44 + tvNameOfActivity.setText(activityName);
37 45  
38 46 addNewEvaluation = findViewById(R.id.btn_add_new_evaluation);
39   - addNewEvaluation.setOnClickListener(view -> startActivity(new Intent(this, NewEvaluation.class)));
  47 + intent.setClass(this, NewEvaluation.class);
  48 + addNewEvaluation.setOnClickListener(view -> startActivity(intent));
40 49  
41   - //TODO Bytte ut Mockdata på sikt
42   - ArrayList<Evaluation> evaluationList= new ArrayList<>();
43   - dbs.getAllEvaluation(evaluationList);
44   -// evaluationList.add(new Evaluation("Tråkke over", "Skade foten", "Velge riktig skotøy", 1, 3));
45   -// evaluationList.add(new Evaluation("Falle i elva", "Blir våt", "Ikke gjør det", 1, 1));
  50 + ArrayList<Evaluation> evaluationList = new ArrayList<>();
  51 + dbs.getAllEvaluationsForActivity(activityId, evaluationList);
46 52 insertEvaluations(evaluationList);
47 53 }
48 54  
... ... @@ -51,7 +57,7 @@ public class Evaluations extends AppCompatActivity {
51 57 lv = (ListView) findViewById(R.id.lv_evaluations);
52 58 lv.setAdapter(new ListAdapterEvaluations(this, evaluations));
53 59 lv.setOnItemClickListener((parent, view, position, id) -> {
54   -// chosenEvaluation = ((Evaluation) lv.getItemAtPosition(position)).getTitle();
  60 + chosenEvaluation = ((Evaluation) lv.getItemAtPosition(position)).getTitle();
55 61 showEvaluationPopup((Evaluation) lv.getItemAtPosition(position));
56 62 });
57 63 }
... ...
Risiko/app/src/main/java/activity/NewEvaluation.java
1 1 package activity;
2 2  
  3 +import android.content.Intent;
3 4 import android.support.v7.app.AppCompatActivity;
4 5 import android.os.Bundle;
5   -import android.view.View;
6 6 import android.widget.ArrayAdapter;
7 7 import android.widget.Button;
8 8 import android.widget.EditText;
9 9 import android.widget.Spinner;
10   -import android.widget.TextView;
11 10  
12 11 import com.risiko.risiko.R;
13 12  
... ... @@ -24,6 +23,9 @@ public class NewEvaluation extends AppCompatActivity {
24 23 protected void onCreate(Bundle savedInstanceState) {
25 24 super.onCreate(savedInstanceState);
26 25 setContentView(R.layout.activity_new_evaluation);
  26 + Intent intent = getIntent();
  27 + int activityId = intent.getIntExtra("ID_OF_CHOSEN_ACTIVITY", -1);
  28 +
27 29  
28 30 dbs = new DatabaseSource(this);
29 31 etNewEvalTitle = findViewById(R.id.et_newEval_title);
... ... @@ -35,10 +37,10 @@ public class NewEvaluation extends AppCompatActivity {
35 37 consequenceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
36 38 consequenceSpinner.setAdapter(consequenceAdapter);
37 39  
38   - Spinner associatedRiskSpinner = (Spinner) findViewById(R.id.spinner_associated_risk);
39   - ArrayAdapter<CharSequence> associatedRiskAdapter = ArrayAdapter.createFromResource(this, R.array.associated_risk_array, android.R.layout.simple_spinner_item);
  40 + Spinner probabilitySpinner = (Spinner) findViewById(R.id.spinner_probability);
  41 + ArrayAdapter<CharSequence> probabilityAdapter = ArrayAdapter.createFromResource(this, R.array.probability_array, android.R.layout.simple_spinner_item);
40 42 consequenceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
41   - associatedRiskSpinner.setAdapter(associatedRiskAdapter);
  43 + probabilitySpinner.setAdapter(probabilityAdapter);
42 44  
43 45 btnNewEval = findViewById(R.id.btn_new_eval);
44 46  
... ... @@ -47,15 +49,40 @@ public class NewEvaluation extends AppCompatActivity {
47 49 String title = etNewEvalTitle.getText().toString();
48 50 String outcome = etNewEvalOutcome.getText().toString();
49 51 String action = etNewEvalAction.getText().toString();
  52 + String consequence = consequenceSpinner.getSelectedItem().toString();
  53 + String probability = probabilitySpinner.getSelectedItem().toString();
  54 +
  55 + int consequenceValue = getConsequenceValue(consequence);
  56 + int probabilityValue = getProbabilityValue(probability);
50 57  
51   - //TODO: Finish this
52   - Evaluation newEval = new Evaluation(title,outcome,action,)
53   - dbs.insertEvaluation()
  58 + Evaluation newEval = new Evaluation(activityId, title, outcome, action, consequenceValue, probabilityValue);
  59 + dbs.insertEvaluation(newEval);
  60 +
  61 + intent.setClass(this, Evaluations.class);
  62 + startActivity(intent);
54 63 }
55 64 });
56 65  
57 66 }
58 67  
  68 + private int getConsequenceValue(String consequence) {
  69 + switch (consequence) {
  70 + case "Ufarlig": return 1;
  71 + case "Moderate": return 2;
  72 + case "Alvorlig": return 3;
  73 + default: return -1;
  74 + }
  75 + }
  76 +
  77 + private int getProbabilityValue(String probability) {
  78 + switch (probability) {
  79 + case "Lav": return 1;
  80 + case "Middels": return 2;
  81 + case "Stor": return 3;
  82 + default: return -1;
  83 + }
  84 + }
  85 +
59 86 private boolean inputAccepted() {
60 87 if (etNewEvalTitle.getText().toString().isEmpty()) return false;
61 88 if (etNewEvalAction.getText().toString().isEmpty()) return false;
... ...
Risiko/app/src/main/java/adapters/ListAdapterEvaluations.java
... ... @@ -29,10 +29,10 @@ public class ListAdapterEvaluations extends ArrayAdapter&lt;Evaluation&gt; {
29 29 evaluationHeadline.setText(evaluation.getTitle());
30 30  
31 31 TextView probability = convertView.findViewById(R.id.tv_probability);
32   - probability.setText(evaluation.getProbability() + "");
  32 + probability.setText(evaluation.getProbabilityString());
33 33  
34 34 TextView consequence = convertView.findViewById(R.id.tv_consequence);
35   - consequence.setText(evaluation.getConsequence() + "");
  35 + consequence.setText(evaluation.getConsequenceString());
36 36  
37 37 return convertView;
38 38 }
... ...
Risiko/app/src/main/java/database/DatabaseSource.java
... ... @@ -62,6 +62,19 @@ public class DatabaseSource {
62 62 database.close();
63 63 }
64 64  
  65 + public void getAllEvaluationsForActivity(int activityId, ArrayList<Evaluation> evaluationsForActivity) {
  66 + SQLiteDatabase database = helper.getWritableDatabase();
  67 + Cursor cursor = database.rawQuery("select * from " + EvaluationTable.EVALUATION_TABLE + " where " + EvaluationTable.EVALUATION_COL_ASSOCIATED_ACTIVITY + " = " + " ?", new String[]{String.valueOf(activityId)});
  68 + cursor.moveToFirst();
  69 + evaluationsForActivity.clear();
  70 + while (!cursor.isAfterLast()) {
  71 + evaluationsForActivity.add(cursorToEvaluation(cursor));
  72 + cursor.moveToNext();
  73 + }
  74 + cursor.close();
  75 + database.close();
  76 + }
  77 +
65 78 public void getAllIncidents(ArrayList<Incident> incidents) {
66 79 SQLiteDatabase database = helper.getWritableDatabase();
67 80 Cursor cursor = database.rawQuery("select * from " + IncidentTable.INCIDENT_TABLE, null);
... ...
Risiko/app/src/main/java/database/IncidentTable.java
... ... @@ -15,6 +15,7 @@ public class IncidentTable {
15 15 private static final String INCIDENT_TABLE_CREATE =
16 16 "CREATE TABLE IF NOT EXISTS " + INCIDENT_TABLE + " ("
17 17 + INCIDENT_COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
  18 + + INCIDENT_COL_ASSOCIATED_EVALUATION + " INTEGER, "
18 19 + INCIDENT_COL_TITLE + " TEXT, "
19 20 + INCIDENT_COL_DATE + " TEXT, "
20 21 + INCIDENT_COL_LOCATION + " TEXT, "
... ...
Risiko/app/src/main/java/dataclasses/Evaluation.java
... ... @@ -6,6 +6,9 @@ package dataclasses;
6 6  
7 7 public class Evaluation {
8 8  
  9 + public static final int CONSEQUENCE_HARMLESS = 1, CONSEQUENCE_MODERATE = 2, CONSEQUENCE_SEVERE = 3;
  10 + public static final int PROBABILITY_LOW = 1, PROBABILITY_MEDIUM = 2, PROBABILITY_HIGH = 3;
  11 +
9 12 int id;
10 13 int associatedActivity;
11 14 String title;
... ... @@ -17,8 +20,9 @@ public class Evaluation {
17 20 public Evaluation() {
18 21 }
19 22  
20   - public Evaluation(String title, String outcome, String measures, int consequence, int probability) {
  23 + public Evaluation(int associatedActivity, String title, String outcome, String measures, int consequence, int probability) {
21 24 this.title = title;
  25 + this.associatedActivity = associatedActivity;
22 26 this.outcome = outcome;
23 27 this.measures = measures;
24 28 this.consequence = consequence;
... ... @@ -61,6 +65,15 @@ public class Evaluation {
61 65 return consequence;
62 66 }
63 67  
  68 + public String getConsequenceString() {
  69 + switch (consequence) {
  70 + case CONSEQUENCE_HARMLESS: return "Ufarlig";
  71 + case CONSEQUENCE_MODERATE: return "Moderate";
  72 + case CONSEQUENCE_SEVERE: return "Alvorlig";
  73 + default: return "Ukjent";
  74 + }
  75 + }
  76 +
64 77 public void setConsequence(int consequence) {
65 78 this.consequence = consequence;
66 79 }
... ... @@ -69,6 +82,15 @@ public class Evaluation {
69 82 return probability;
70 83 }
71 84  
  85 + public String getProbabilityString() {
  86 + switch (probability) {
  87 + case PROBABILITY_LOW: return "Lav";
  88 + case PROBABILITY_MEDIUM: return "Middels";
  89 + case PROBABILITY_HIGH: return "Stor";
  90 + default: return "Ukjent";
  91 + }
  92 + }
  93 +
72 94 public void setProbability(int probability) {
73 95 this.probability = probability;
74 96 }
... ...
Risiko/app/src/main/res/layout/activity_new_evaluation.xml
... ... @@ -78,16 +78,16 @@
78 78 android:orientation="horizontal">
79 79  
80 80 <TextView
81   - android:layout_marginEnd="20dp"
82   - android:id="@+id/tv_associated_risk"
  81 + android:id="@+id/tv_probability"
83 82 android:layout_width="wrap_content"
84 83 android:layout_height="wrap_content"
  84 + android:layout_marginEnd="20dp"
  85 + android:text="@string/probability"
85 86 android:textSize="17sp"
86   - android:textStyle="bold"
87   - android:text="@string/probability" />
  87 + android:textStyle="bold" />
88 88  
89 89 <Spinner
90   - android:id="@+id/spinner_associated_risk"
  90 + android:id="@+id/spinner_probability"
91 91 android:layout_width="fill_parent"
92 92 android:layout_height="wrap_content" />
93 93  
... ...
Risiko/app/src/main/res/values/array.xml
1 1 <?xml version="1.0" encoding="utf-8"?>
2 2 <resources>
3 3  
4   - <string-array name="consequence_array">
5   - <item>Ufarlig</item>
6   - <item>Moderate</item>
7   - <item>Alvorlig</item>
8   - </string-array>
  4 + <string-array name="consequence_array">
  5 + <item>Ufarlig</item>
  6 + <item>Moderate</item>
  7 + <item>Alvorlig</item>
  8 + </string-array>
9 9  
10   - <string-array name="associated_risk_array">
  10 + <string-array name="probability_array">
11 11 <item>Lav</item>
12 12 <item>Middels</item>
13 13 <item>Stor</item>
... ...