Commit 615050cc3a082f6e6982861d9757250936d34659

Authored by Ali B
1 parent 7a2d67a2

Started working on Incidents and new Incidents. Starting connecting the SQLite d…

…atabase to the Incident class and save incidents in the database
Risiko/app/src/main/java/activity/Incident.java
... ... @@ -14,10 +14,12 @@ import java.util.ArrayList;
14 14 import java.util.List;
15 15  
16 16 import adapters.ListAdapterIncidents;
  17 +import database.DatabaseSource;
17 18  
18 19 public class Incident extends AppCompatActivity {
19 20 ListView lv;
20 21 int chosenEventId;
  22 + DatabaseSource dbs;
21 23  
22 24 @Override
23 25 protected void onCreate(Bundle savedInstanceState) {
... ... @@ -25,14 +27,17 @@ public class Incident extends AppCompatActivity {
25 27 setContentView(R.layout.activity_incidents);
26 28  
27 29  
  30 +
28 31 //TODO Bytte ut Mockdata på sikt
29 32 ArrayList<dataclasses.Incident> incidentList = new ArrayList<>();
30   - incidentList.add(new dataclasses.Incident("Datt i elva"));
31   - incidentList.add(new dataclasses.Incident("Brant meg under matlaging"));
32   - incidentList.add(new dataclasses.Incident("Vrikket foten"));
33   - incidentList.add(new dataclasses.Incident("Falt ned fra et bord"));
34   - incidentList.add(new dataclasses.Incident("Kræsjet i et tre"));
35   - incidentList.add(new dataclasses.Incident("Fikk alkoholforgiftning"));
  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"));
36 41 insertEvents(incidentList);
37 42 }
38 43  
... ... @@ -40,13 +45,10 @@ public class Incident extends AppCompatActivity {
40 45 private void insertEvents(List<dataclasses.Incident> incident) {
41 46 lv = (ListView) findViewById(R.id.lv_events);
42 47 lv.setAdapter(new ListAdapterIncidents(this, incident));
43   - lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
44   - @Override
45   - public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
46   - chosenEventId = ((dataclasses.Incident) lv.getItemAtPosition(position)).getId();
47   - showEventPopup();
  48 + lv.setOnItemClickListener((parent, view, position, id) -> {
  49 + chosenEventId = ((dataclasses.Incident) lv.getItemAtPosition(position)).getId();
  50 + showEventPopup();
48 51  
49   - }
50 52 });
51 53 }
52 54  
... ...
Risiko/app/src/main/java/activity/NewIncident.java
... ... @@ -2,14 +2,36 @@ package activity;
2 2  
3 3 import android.support.v7.app.AppCompatActivity;
4 4 import android.os.Bundle;
  5 +import android.widget.ArrayAdapter;
  6 +import android.widget.EditText;
  7 +import android.widget.Spinner;
5 8  
6 9 import com.risiko.risiko.R;
7 10  
  11 +import java.util.ArrayList;
  12 +import java.util.List;
  13 +
  14 +import database.DatabaseSource;
  15 +import dataclasses.Incident;
  16 +
8 17 public class NewIncident extends AppCompatActivity {
9 18  
  19 + private EditText etDate, etLocation, etTitle, etDescripton;
  20 + private Spinner spinnerAssociatedRisk;
  21 + private DatabaseSource dbs;
  22 +
10 23 @Override
11 24 protected void onCreate(Bundle savedInstanceState) {
12 25 super.onCreate(savedInstanceState);
13 26 setContentView(R.layout.activity_new_incident);
  27 + dbs = new DatabaseSource(this);
  28 +
  29 + ArrayList<Incident> incidents = new ArrayList<>();
  30 + dbs.getAllIncidents(incidents);
  31 +
  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);
  34 + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  35 + spinnerAssociatedRisk.setAdapter(adapter);
14 36 }
15 37 }
... ...
Risiko/app/src/main/java/database/DatabaseSource.java
... ... @@ -49,39 +49,49 @@ public class DatabaseSource {
49 49 return insertId > -1;
50 50 }
51 51  
52   - public void getAllEvaluation(ArrayList<Evaluation> evaluations) {
  52 + public void getAllEvaluation(ArrayList<Evaluation> listToUpdate) {
53 53 SQLiteDatabase database = helper.getWritableDatabase();
54 54 Cursor cursor = database.rawQuery("select * from " + EvaluationTable.EVALUATION_TABLE, null);
55 55 cursor.moveToFirst();
56   - evaluations.clear();
  56 + listToUpdate.clear();
57 57 while (!cursor.isAfterLast()) {
58   - evaluations.add(cursorToEvaluation(cursor));
  58 + listToUpdate.add(cursorToEvaluation(cursor));
59 59 cursor.moveToNext();
60 60 }
61 61 cursor.close();
62 62 database.close();
63 63 }
64 64  
65   - public void getAllEvaluationsForActivity(int activityId, ArrayList<Evaluation> evaluationsForActivity) {
  65 + public Evaluation getEvaluationById(int id) {
  66 + SQLiteDatabase database = helper.getWritableDatabase();
  67 + Cursor cursor = database.rawQuery("select * from " + EvaluationTable.EVALUATION_TABLE + " where " + EvaluationTable.EVALUATION_COL_ID + " = " + " ?", new String[]{String.valueOf(id)});
  68 + cursor.moveToFirst();
  69 + if (!cursor.isAfterLast())
  70 + return cursorToEvaluation(cursor);
  71 +
  72 + return new Evaluation();
  73 + }
  74 +
  75 + public void getAllEvaluationsForActivity(int activityId, ArrayList<Evaluation> listToUpdate) {
66 76 SQLiteDatabase database = helper.getWritableDatabase();
67 77 Cursor cursor = database.rawQuery("select * from " + EvaluationTable.EVALUATION_TABLE + " where " + EvaluationTable.EVALUATION_COL_ASSOCIATED_ACTIVITY + " = " + " ?", new String[]{String.valueOf(activityId)});
68 78 cursor.moveToFirst();
69   - evaluationsForActivity.clear();
  79 + listToUpdate.clear();
70 80 while (!cursor.isAfterLast()) {
71   - evaluationsForActivity.add(cursorToEvaluation(cursor));
  81 + listToUpdate.add(cursorToEvaluation(cursor));
72 82 cursor.moveToNext();
73 83 }
74 84 cursor.close();
75 85 database.close();
76 86 }
77 87  
78   - public void getAllIncidents(ArrayList<Incident> incidents) {
  88 + public void getAllIncidents(ArrayList<Incident> listToUpdate) {
79 89 SQLiteDatabase database = helper.getWritableDatabase();
80 90 Cursor cursor = database.rawQuery("select * from " + IncidentTable.INCIDENT_TABLE, null);
81 91 cursor.moveToFirst();
82   - incidents.clear();
  92 + listToUpdate.clear();
83 93 while (!cursor.isAfterLast()) {
84   - incidents.add(cursorToIncident(cursor));
  94 + listToUpdate.add(cursorToIncident(cursor));
85 95 cursor.moveToNext();
86 96 }
87 97 cursor.close();
... ...
Risiko/app/src/main/res/layout/activity_new_incident.xml
... ... @@ -22,30 +22,34 @@
22 22 app:layout_constraintTop_toTopOf="parent"/>
23 23  
24 24 <EditText
  25 + android:id="@+id/et_newIncident_date"
25 26 android:layout_width="match_parent"
26 27 android:layout_height="wrap_content"
27 28 android:hint="Dato:"
28   - android:inputType="date"/>
  29 + android:inputType="date" />
29 30  
30 31 <EditText
  32 + android:id="@+id/et_newIncident_location"
31 33 android:layout_width="match_parent"
32 34 android:layout_height="wrap_content"
33 35 android:hint="Sted:"
34   - android:inputType="text"/>
  36 + android:inputType="text" />
35 37  
36 38 <EditText
  39 + android:id="@+id/et_newIncident_title"
37 40 android:layout_width="match_parent"
38 41 android:layout_height="wrap_content"
39 42 android:hint="Tittel:"
40   - android:inputType="text"/>
  43 + android:inputType="text" />
41 44  
42 45 <EditText
  46 + android:id="@+id/et_newIncident_description"
43 47 android:layout_width="match_parent"
44 48 android:layout_height="wrap_content"
45 49 android:hint="Beskrivelse:"
46   - android:minLines="2"
  50 + android:inputType="textMultiLine"
47 51 android:maxLines="5"
48   - android:inputType="textMultiLine"/>
  52 + android:minLines="2" />
49 53  
50 54 <LinearLayout
51 55 android:padding="10dp"
... ... @@ -69,11 +73,12 @@
69 73  
70 74 </LinearLayout>
71 75  
72   - <Button
73   - android:layout_gravity="center"
74   - android:layout_width="wrap_content"
75   - android:layout_height="wrap_content"
76   - android:text="Lagre hendelse"/>
  76 + <Button
  77 + android:id="@+id/btn_newIncident"
  78 + android:layout_width="wrap_content"
  79 + android:layout_height="wrap_content"
  80 + android:layout_gravity="center"
  81 + android:text="Lagre hendelse" />
77 82  
78 83 </LinearLayout>
79 84  
... ...