Browse Source

50% working DB, new Constructor in Student Class,

master
MrTob 5 years ago
parent
commit
bb2a2be7f1
7 changed files with 159 additions and 100 deletions
  1. +2
    -0
      app/build.gradle
  2. +9
    -0
      app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/DrinkAdapter.java
  3. +0
    -69
      app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/FileHelper.java
  4. +13
    -14
      app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/MainActivity.java
  5. +22
    -2
      app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/Student.java
  6. +108
    -14
      app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/UserDB.java
  7. +5
    -1
      build.gradle

+ 2
- 0
app/build.gradle View File

@@ -25,4 +25,6 @@ dependencies {
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
debugImplementation 'com.idescout.sql:sqlscout-server:4.1'
releaseImplementation 'com.idescout.sql:sqlscout-server-noop:4.1'
} }

+ 9
- 0
app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/DrinkAdapter.java View File

@@ -2,6 +2,7 @@ package at.ac.perg.htl.bhif16.coffeeapp;


import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@@ -19,6 +20,7 @@ public class DrinkAdapter extends ArrayAdapter<Student> {
Context context; Context context;
int layoutResourceId; int layoutResourceId;
int i; int i;

ArrayList<Student> students = new ArrayList<Student>(); ArrayList<Student> students = new ArrayList<Student>();


public DrinkAdapter(Context context, int layoutResourceId, public DrinkAdapter(Context context, int layoutResourceId,
@@ -27,8 +29,11 @@ public class DrinkAdapter extends ArrayAdapter<Student> {
this.layoutResourceId = layoutResourceId; this.layoutResourceId = layoutResourceId;
this.context = context; this.context = context;
this.students = studs; this.students = studs;


} }



@Override @Override
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView; View item = convertView;
@@ -61,6 +66,10 @@ public class DrinkAdapter extends ArrayAdapter<Student> {


finalStudentWrapper.consumedDrinks.setText(student.getConsumedDrinks()+""); finalStudentWrapper.consumedDrinks.setText(student.getConsumedDrinks()+"");
student.setConsumedDrinks(student.getConsumedDrinks()+1); student.setConsumedDrinks(student.getConsumedDrinks()+1);




} }
}); });




+ 0
- 69
app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/FileHelper.java View File

@@ -1,69 +0,0 @@
package at.ac.perg.htl.bhif16.coffeeapp;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileHelper {
final static String fileName = "data.txt";
final static String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/coffeeapp/readwrite/" ;
final static String TAG = FileHelper.class.getName();

public static String ReadFile( Context context){
String line = null;

try {
FileInputStream fileInputStream = new FileInputStream (new File(path + fileName));
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();

while ( (line = bufferedReader.readLine()) != null )
{
stringBuilder.append(line + System.getProperty("line.separator"));
}
fileInputStream.close();
line = stringBuilder.toString();

bufferedReader.close();
}
catch(FileNotFoundException ex) {
Log.d(TAG, ex.getMessage());
}
catch(IOException ex) {
Log.d(TAG, ex.getMessage());
}
return line;
}

public static boolean saveToFile( String data){
try {
new File(path ).mkdir();
File file = new File(path+ fileName);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(file,true);
fileOutputStream.write((data + System.getProperty("line.separator")).getBytes());

return true;
} catch(FileNotFoundException ex) {
Log.d(TAG, ex.getMessage());
} catch(IOException ex) {
Log.d(TAG, ex.getMessage());
}
return false;


}

}


+ 13
- 14
app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/MainActivity.java View File

@@ -8,6 +8,7 @@ import android.graphics.drawable.Drawable;
import android.media.Image; import android.media.Image;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@@ -17,17 +18,12 @@ import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.BaseAdapter; import android.widget.BaseAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;


import org.w3c.dom.Text;


import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



public class MainActivity extends Activity { public class MainActivity extends Activity {


@@ -45,13 +41,16 @@ public class MainActivity extends Activity {


//TODO //TODO
//add students //add students
studentArray.add(new Student("Achleitner",0,R.drawable.achleitner));
studentArray.add(new Student("Aichinger",0,R.drawable.aichinger));
studentArray.add(new Student("Bodingbauer",0,R.drawable.bodingbauer));
studentArray.add(new Student("Bogdan",0,R.drawable.bogdan));
studentArray.add(new Student("Derntl",0,R.drawable.derntl));
studentArray.add(new Student("Diesenreiter",0,R.drawable.unknown));
studentArray.add(new Student("Forthofer",0,R.drawable.forthofer));
studentArray.add(new Student(0,"Achleitner",0,R.drawable.achleitner));
studentArray.add(new Student(1,"Aichinger",0,R.drawable.aichinger));
studentArray.add(new Student(2,"Bodingbauer",0,R.drawable.bodingbauer));
studentArray.add(new Student(3,"Bogdan",0,R.drawable.bogdan));
studentArray.add(new Student(4,"Derntl",0,R.drawable.derntl));
studentArray.add(new Student(5,"Diesenreiter",0,R.drawable.unknown));
studentArray.add(new Student(6,"Forthofer",0,R.drawable.forthofer));

UserDB db = new UserDB(this);
db.createDefaultNotesIfNeed();


studentArrayAdapter = new DrinkAdapter(MainActivity.this, R.layout.listview_activity, studentArray); studentArrayAdapter = new DrinkAdapter(MainActivity.this, R.layout.listview_activity, studentArray);
listview= (ListView) findViewById(R.id.list_view); listview= (ListView) findViewById(R.id.list_view);


+ 22
- 2
app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/Student.java View File

@@ -1,16 +1,29 @@
package at.ac.perg.htl.bhif16.coffeeapp; package at.ac.perg.htl.bhif16.coffeeapp;


public class Student {
import java.io.Serializable;

public class Student implements Serializable {

private int studentId;
private String name; private String name;
private int img; private int img;
private int consumedDrinks; private int consumedDrinks;


public Student(String name, int consumedDrinks, int img) {

public Student(int studentId, String name, int consumedDrinks, int img) {


this.name = name; this.name = name;
this.consumedDrinks = consumedDrinks; this.consumedDrinks = consumedDrinks;
this.img = img; this.img = img;
this.studentId = studentId;
}

public int getStudentId() {
return studentId;
}


public void setStudentId(int studentId) {
this.studentId = studentId;
} }


public int getImage() { public int getImage() {
@@ -27,6 +40,8 @@ public class Student {
} }






public int getConsumedDrinks() { public int getConsumedDrinks() {
return consumedDrinks; return consumedDrinks;
} }
@@ -34,4 +49,9 @@ public class Student {
public void setConsumedDrinks(int consumedDrinks) { public void setConsumedDrinks(int consumedDrinks) {
this.consumedDrinks = consumedDrinks; this.consumedDrinks = consumedDrinks;
} }

@Override
public String toString(){
return this.name;
}
} }

+ 108
- 14
app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/UserDB.java View File

@@ -1,6 +1,8 @@
package at.ac.perg.htl.bhif16.coffeeapp; package at.ac.perg.htl.bhif16.coffeeapp;


import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log; import android.util.Log;
@@ -10,28 +12,120 @@ import static android.content.Context.MODE_PRIVATE;
public class UserDB extends SQLiteOpenHelper { public class UserDB extends SQLiteOpenHelper {


private static final String DATABASE_NAME = "USERS.db"; private static final String DATABASE_NAME = "USERS.db";

private static final int DATABASE_VERSION = 1; private static final int DATABASE_VERSION = 1;
private static final String TABLE_STUDENTS = "STUDENTS";


// Database creation sql statement
private static final String create_table = "CREATE TABLE IF NOT EXISTS students(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, drink INTEGER);";
private static final String COLUMN_STUDENT_ID = "Student_Id";
private static final String COLUMN_STUDENT_NAME = "Student_Name";
private static final String COLUMN_STUDENT_COUNT_DRINKS = "Student_Count";


public UserDB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
public UserDB(Context context){
super(context, DATABASE_NAME,null,DATABASE_VERSION);
} }


// Method is called during creation of the database
//create Table
@Override @Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(create_table);
public void onCreate(SQLiteDatabase db) {
Log.i("SQLite","GOOO...");
String query = "CREATE TABLE " + TABLE_STUDENTS + "("
+ COLUMN_STUDENT_ID + " INTEGER PRIMARY KEY," + COLUMN_STUDENT_NAME + " TEXT,"
+ COLUMN_STUDENT_COUNT_DRINKS + " INTEGER" + ")";
db.execSQL(query);
} }


// Method is called during an upgrade of the database,
@Override @Override
public void onUpgrade(SQLiteDatabase database,int oldVersion,int newVersion){
Log.w(UserDB.class.getName(),"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
database.execSQL("DROP TABLE IF EXISTS FridgeItem");
onCreate(database);
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i("SQLite", "MyDatabaseHelper.onUpgrade ... ");
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENTS);

// Create tables again
onCreate(db);
}

public void createDefaultNotesIfNeed() {
int count = this.getNotesCount();
if(count ==0 ) {
Student s1 = new Student(0,"Achleitner",0,0);
Student s2 = new Student(1,"Aichinger",2,0);

this.addNote(s1);
this.addNote(s2);
this.updateStudent(s1);
}

}

public int getNotesCount() {
Log.i("SQLite", "MyDatabaseHelper.getNotesCount ... " );

String countQuery = "SELECT * FROM " + TABLE_STUDENTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);

int count = cursor.getCount();

cursor.close();

// return count
return count;
}

public Student getNote(int id) {
Log.i("SQLite", "MyDatabaseHelper.getNote ... " + id);

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(TABLE_STUDENTS, new String[] { COLUMN_STUDENT_ID,
COLUMN_STUDENT_NAME, COLUMN_STUDENT_COUNT_DRINKS }, COLUMN_STUDENT_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();

Student note = new Student(Integer.parseInt(cursor.getString(0)),cursor.getString(1),Integer.parseInt(cursor.getString(2)),Integer.parseInt(cursor.getString(3)));

// return note
return note;
} }

public void addNote(Student note) {
Log.i("SQLite", "MyDatabaseHelper.addNote ... " + note.getName());

SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(COLUMN_STUDENT_ID, note.getStudentId());
values.put(COLUMN_STUDENT_NAME, note.getName());
values.put(COLUMN_STUDENT_COUNT_DRINKS, note.getConsumedDrinks());

// Inserting Row
db.insert(TABLE_STUDENTS, null, values);

// Closing database connection
db.close();
}

public void updateStudent(Student note) {
Log.i("SQLite", "MyDatabaseHelper.update ... " + note.getName());

SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(COLUMN_STUDENT_ID, note.getStudentId());
values.put(COLUMN_STUDENT_NAME, note.getName());
values.put(COLUMN_STUDENT_COUNT_DRINKS, note.getConsumedDrinks());

// Inserting Row
db.update(TABLE_STUDENTS,values,"Student_Id="+note.getStudentId(),null);
Log.i("SQLite", "ANAL ... ");

// Closing database connection
db.close();
}






} }

+ 5
- 1
build.gradle View File

@@ -18,7 +18,11 @@ allprojects {
repositories { repositories {
google() google()
jcenter() jcenter()
maven {
url 'http://www.idescout.com/maven/repo/'
name 'IDEScout, Inc.'
}

} }
} }




Loading…
Cancel
Save