Commit 7e4b1ed5f5205a0d80a994b0a4a983f6e6f4f8b1

Authored by Ali B
1 parent d87500ec

Finished Incidents and new Incidents so you now can add new incidents and see in…

…cidents that are saved in the database. The app is complete at this point, it only need some polishing
Risiko/app/src/main/java/activity/Evaluations.java
... ... @@ -45,7 +45,10 @@ public class Evaluations extends AppCompatActivity {
45 45  
46 46 addNewEvaluation = findViewById(R.id.btn_add_new_evaluation);
47 47 intent.setClass(this, NewEvaluation.class);
48   - addNewEvaluation.setOnClickListener(view -> startActivity(intent));
  48 + addNewEvaluation.setOnClickListener(view -> {
  49 + finish();
  50 + startActivity(intent);
  51 + });
49 52  
50 53 ArrayList<Evaluation> evaluationList = new ArrayList<>();
51 54 dbs.getAllEvaluationsForActivity(activityId, evaluationList);
... ... @@ -69,9 +72,6 @@ public class Evaluations extends AppCompatActivity {
69 72 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
70 73 alertDialogBuilder.setView(promptView);
71 74  
72   - //TODO FINNE EN MÅTE Å KNYTTE SAMMEN POPUP MED DET ELEMENTET SOM
73   -// Evaluation evaluati = new Evaluation("Tråkke over", "Skade foten", "Velge riktig skotøy", 1, 2);
74   -
75 75  
76 76 TextView headline = promptView.findViewById(R.id.pop_headline);
77 77 TextView outcome = promptView.findViewById(R.id.pop_possible_outcomes);
... ...
Risiko/app/src/main/java/activity/Incident.java
... ... @@ -7,6 +7,7 @@ import android.view.LayoutInflater;
7 7 import android.view.View;
8 8 import android.widget.AdapterView;
9 9 import android.widget.ListView;
  10 +import android.widget.TextView;
10 11  
11 12 import com.risiko.risiko.R;
12 13  
... ... @@ -15,6 +16,7 @@ import java.util.List;
15 16  
16 17 import adapters.ListAdapterIncidents;
17 18 import database.DatabaseSource;
  19 +import dataclasses.Evaluation;
18 20  
19 21 public class Incident extends AppCompatActivity {
20 22 ListView lv;
... ... @@ -25,19 +27,10 @@ public class Incident extends AppCompatActivity {
25 27 protected void onCreate(Bundle savedInstanceState) {
26 28 super.onCreate(savedInstanceState);
27 29 setContentView(R.layout.activity_incidents);
  30 + dbs = new DatabaseSource(this);
28 31  
29   -
30   -
31   - //TODO Bytte ut Mockdata på sikt
32 32 ArrayList<dataclasses.Incident> incidentList = new ArrayList<>();
33 33 dbs.getAllIncidents(incidentList);
34   -
35   -// incidentList.add(new dataclasses.Incident("Datt i elva"));
36   -// incidentList.add(new dataclasses.Incident("Brant meg under matlaging"));
37   -// incidentList.add(new dataclasses.Incident("Vrikket foten"));
38   -// incidentList.add(new dataclasses.Incident("Falt ned fra et bord"));
39   -// incidentList.add(new dataclasses.Incident("Kræsjet i et tre"));
40   -// incidentList.add(new dataclasses.Incident("Fikk alkoholforgiftning"));
41 34 insertEvents(incidentList);
42 35 }
43 36  
... ... @@ -47,22 +40,32 @@ public class Incident extends AppCompatActivity {
47 40 lv.setAdapter(new ListAdapterIncidents(this, incident));
48 41 lv.setOnItemClickListener((parent, view, position, id) -> {
49 42 chosenEventId = ((dataclasses.Incident) lv.getItemAtPosition(position)).getId();
50   - showEventPopup();
  43 + showEventPopup((dataclasses.Incident) lv.getItemAtPosition(position));
51 44  
52 45 });
53 46 }
54 47  
55   - private void showEventPopup() {
  48 + private void showEventPopup(dataclasses.Incident chosenItem) {
56 49 LayoutInflater layoutInflater = LayoutInflater.from(this);
57 50 View promptView = layoutInflater.inflate(R.layout.popup_event, null);
58 51 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
59 52 alertDialogBuilder.setView(promptView);
60 53  
  54 + TextView tvDate = promptView.findViewById(R.id.pop_date);
  55 + TextView tvLocation = promptView.findViewById(R.id.pop_place);
  56 + TextView tvDescription = promptView.findViewById(R.id.pop_description);
  57 + TextView tvAssociatedRisk = promptView.findViewById(R.id.pop_associated_risk);
  58 +
  59 + tvDate.setText(chosenItem.getDate());
  60 + tvLocation.setText(chosenItem.getPlaceOfEvent());
  61 + tvDescription.setText(chosenItem.getDescription());
  62 + Evaluation associatedEvaluation = dbs.getEvaluationById(chosenItem.getAssociatedEvaluation());
  63 + tvAssociatedRisk.setText(associatedEvaluation.getTitle());
  64 +
61 65 alertDialogBuilder.setCancelable(false)
62 66 .setNegativeButton(getString(R.string.cancel),
63 67 (dialog, id) -> dialog.cancel());
64 68 AlertDialog alert = alertDialogBuilder.create();
65   -
66 69 alert.show();
67 70  
68 71 }
... ...
Risiko/app/src/main/java/activity/NewEvaluation.java
... ... @@ -58,8 +58,9 @@ public class NewEvaluation extends AppCompatActivity {
58 58 Evaluation newEval = new Evaluation(activityId, title, outcome, action, consequenceValue, probabilityValue);
59 59 dbs.insertEvaluation(newEval);
60 60  
61   - intent.setClass(this, Evaluations.class);
62   - startActivity(intent);
  61 +// intent.setClass(this, Evaluations.class);
  62 +// startActivity(intent);
  63 + finish();
63 64 }
64 65 });
65 66  
... ...
Risiko/app/src/main/java/activity/NewIncident.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;
  6 +import android.util.Log;
5 7 import android.widget.ArrayAdapter;
  8 +import android.widget.Button;
6 9 import android.widget.EditText;
7 10 import android.widget.Spinner;
8 11  
9 12 import com.risiko.risiko.R;
10 13  
11 14 import java.util.ArrayList;
  15 +import java.util.Dictionary;
  16 +import java.util.HashMap;
12 17 import java.util.List;
13 18  
14 19 import database.DatabaseSource;
  20 +import dataclasses.Evaluation;
15 21 import dataclasses.Incident;
16 22  
17 23 public class NewIncident extends AppCompatActivity {
18 24  
19 25 private EditText etDate, etLocation, etTitle, etDescripton;
20 26 private Spinner spinnerAssociatedRisk;
  27 + private Button btnNewIncident;
21 28 private DatabaseSource dbs;
22 29  
23 30 @Override
24 31 protected void onCreate(Bundle savedInstanceState) {
25 32 super.onCreate(savedInstanceState);
26 33 setContentView(R.layout.activity_new_incident);
27   - dbs = new DatabaseSource(this);
28 34  
29   - ArrayList<Incident> incidents = new ArrayList<>();
30   - dbs.getAllIncidents(incidents);
  35 + etDate = findViewById(R.id.et_newIncident_date);
  36 + etLocation = findViewById(R.id.et_newIncident_location);
  37 + etTitle = findViewById(R.id.et_newIncident_title);
  38 + etDescripton = findViewById(R.id.et_newIncident_description);
  39 + btnNewIncident = findViewById(R.id.btn_newIncident);
  40 +
  41 +
  42 + dbs = new DatabaseSource(this);
  43 + ArrayList<Evaluation> evaluations = new ArrayList<>();
  44 + dbs.getAllEvaluation(evaluations);
  45 + HashMap<String, Integer> evalMap = convertToDictionaryWithIdAndTitle(evaluations);
31 46  
32   - Spinner spinnerAssociatedRisk = (Spinner) findViewById(R.id.spinner_associated_risk);
33   - ArrayAdapter<Incident> adapter = new ArrayAdapter<Incident>(this, android.R.layout.simple_spinner_item, incidents);
  47 + spinnerAssociatedRisk = findViewById(R.id.spinner_associated_risk);
  48 + ArrayAdapter<Object> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, evalMap.keySet().toArray());
34 49 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
35 50 spinnerAssociatedRisk.setAdapter(adapter);
  51 +
  52 + btnNewIncident.setOnClickListener(view -> {
  53 + if (inputAccepted()) {
  54 + String title = etTitle.getText().toString();
  55 + String date = etDate.getText().toString();
  56 + String location = etLocation.getText().toString();
  57 + String description = etDescripton.getText().toString();
  58 +
  59 + String associatedRiskTitle = spinnerAssociatedRisk.getSelectedItem().toString();
  60 + int associatedRisk = evalMap.get(associatedRiskTitle);
  61 + System.out.println();
  62 +
  63 + dataclasses.Incident newIncident = new Incident(date, location, title, description, associatedRisk);
  64 + dbs.insertIncident(newIncident);
  65 +
  66 +// Intent intent = new Intent(this, activity.Incident.class);
  67 +// startActivity(intent);
  68 + finish();
  69 + }
  70 + });
  71 +
  72 + }
  73 +
  74 + private boolean inputAccepted() {
  75 + if (etDate.getText().toString().isEmpty()) return false;
  76 + if (etTitle.getText().toString().isEmpty()) return false;
  77 + if (etLocation.getText().toString().isEmpty()) return false;
  78 + if (etDescripton.getText().toString().isEmpty()) return false;
  79 +
  80 + return true;
  81 + }
  82 +
  83 + private HashMap<String, Integer> convertToDictionaryWithIdAndTitle(ArrayList<Evaluation> evaluations) {
  84 + HashMap<String, Integer> evalMap = new HashMap<String, Integer>();
  85 + for (Evaluation e : evaluations) {
  86 + evalMap.put(e.getTitle(), e.getId());
  87 + }
  88 + return evalMap;
36 89 }
37 90 }
... ...
Risiko/app/src/main/java/dataclasses/Incident.java
... ... @@ -18,12 +18,12 @@ public class Incident {
18 18 public Incident() {
19 19 }
20 20  
21   - public Incident(String date, String place, String title, String description, int id) {
  21 + public Incident(String date, String place, String title, String description, int associatedEvaluation) {
22 22 this.date = date;
23 23 this.placeOfEvent = place;
24 24 this.title = title;
25 25 this.description = description;
26   - this.id = id;
  26 + this.associatedEvaluation = associatedEvaluation;
27 27 }
28 28  
29 29 public Incident(String title) {
... ...
Risiko/app/src/main/res/layout/activity_evaluations.xml
... ... @@ -57,10 +57,9 @@
57 57  
58 58 <ListView
59 59 android:id="@+id/lv_evaluations"
60   - android:layout_margin="10dp"
61 60 android:layout_width="match_parent"
62   - android:layout_height="match_parent">
63   - </ListView>
  61 + android:layout_height="369dp"
  62 + android:layout_margin="10dp"></ListView>
64 63  
65 64 </LinearLayout>
66 65 </android.support.v4.widget.SwipeRefreshLayout>
... ...
Risiko/app/src/main/res/layout/activity_new_incident.xml
... ... @@ -25,7 +25,7 @@
25 25 android:id="@+id/et_newIncident_date"
26 26 android:layout_width="match_parent"
27 27 android:layout_height="wrap_content"
28   - android:hint="Dato:"
  28 + android:hint="Dato (dd.mm.yyyy):"
29 29 android:inputType="date" />
30 30  
31 31 <EditText
... ...