Created
April 5, 2017 19:43
-
-
Save AuraLoredana/8af1aa0298a87dcc6bb54b9ffe8f67fe to your computer and use it in GitHub Desktop.
Quizz App for students
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
import android.content.Context; | |
import android.support.v4.view.PagerAdapter; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
import android.widget.LinearLayout; | |
import android.widget.RelativeLayout; | |
import android.widget.TextView; | |
/** | |
* Created by POPESCU on 3/25/2017. | |
*/ | |
public class CustomSwipeAdapter extends PagerAdapter { | |
private int[] image_resources = {R.drawable.cambridge, R.drawable.gmat, R.drawable.pearson, R.drawable.freetest}; | |
private Context ctx; | |
private LayoutInflater layoutInflater; | |
public CustomSwipeAdapter(Context ctx) { | |
this.ctx = ctx; | |
} | |
@Override | |
public int getCount() { | |
return image_resources.length; | |
} | |
@Override | |
public boolean isViewFromObject(View view, Object o) { | |
return (view == (LinearLayout) o); | |
} | |
@Override | |
public Object instantiateItem(ViewGroup container, int position) { | |
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
View item_view = layoutInflater.inflate(R.layout.swipe_layout, container, false); | |
ImageView imageView = (ImageView) item_view.findViewById(R.id.image_view); | |
TextView textView = (TextView) item_view.findViewById(R.id.image_count); | |
imageView.setImageResource(image_resources[position]); | |
textView.setText("Choose your exam:" ); | |
container.addView(item_view); | |
return item_view; | |
} | |
@Override | |
public void destroyItem(ViewGroup container, int position, Object object) { | |
container.removeView((LinearLayout)object); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
/** | |
* Created by POPESCU on 3/26/2017. | |
*/ | |
public class DBHelper extends SQLiteOpenHelper { | |
//version number to upgrade database version | |
//each time if you Add, Edit table, you need to change the | |
//version number. | |
private static final int DATABASE_VERSION = 200; | |
// Database Name | |
private static final String DATABASE_NAME = "crud.db"; | |
public DBHelper(Context context ) { | |
super(context, DATABASE_NAME, null, DATABASE_VERSION); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
//All necessary tables you like to create will create here | |
String CREATE_TABLE_STUDENT = "CREATE TABLE " + Student.TABLE + "(" | |
+ Student.KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," | |
+ Student.KEY_name + " TEXT, " | |
+ Student.KEY_age + " INTEGER, " | |
+ Student.KEY_cnp + " LONG, " | |
+ Student.KEY_email + " TEXT )"; | |
db.execSQL(CREATE_TABLE_STUDENT); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
// Drop older table if existed, all data will be gone!!! | |
db.execSQL("DROP TABLE IF EXISTS " + Student.TABLE); | |
// Create tables again | |
onCreate(db); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.support.v7.app.AlertDialog; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
public class ExitDialog extends AppCompatActivity { | |
Button btnExit; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_exit_dialog); | |
} | |
@Override | |
public void onBackPressed() { | |
AlertDialog.Builder builder = new AlertDialog.Builder(this); | |
builder.setMessage("Are you sure you want to exit?") | |
.setCancelable(false) | |
.setPositiveButton("Yes", new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int id) { | |
ExitDialog.this.finish(); | |
} | |
}) | |
.setNegativeButton("No", new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int id) { | |
dialog.cancel(); | |
} | |
}); | |
AlertDialog alert = builder.create(); | |
alert.show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
public class Main22Activity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main22); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
public class Main2Activity extends AppCompatActivity implements View.OnClickListener { | |
Button next; | |
Button previous; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main2); | |
next = (Button)findViewById(R.id.next); | |
next.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(v.getContext(), Main3Activity.class); | |
startActivity(intent); | |
} | |
}); | |
previous = (Button)findViewById(R.id.previous); | |
previous.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(v.getContext(), MainActivity.class); | |
startActivity(intent); | |
} | |
}); | |
} | |
@Override | |
public void onClick (View v){ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
public class Main3Activity extends AppCompatActivity implements View.OnClickListener { | |
Button next; | |
Button previous; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main3); | |
next = (Button)findViewById(R.id.next); | |
next.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(v.getContext(), Main4Activity.class); | |
startActivity(intent); | |
} | |
}); | |
previous = (Button)findViewById(R.id.previous); | |
previous.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(v.getContext(), Main2Activity.class); | |
startActivity(intent); | |
} | |
}); | |
} | |
@Override | |
public void onClick (View v){ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
public class Main4Activity extends AppCompatActivity implements View.OnClickListener { | |
Button next; | |
Button previous; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main4); | |
next = (Button)findViewById(R.id.next); | |
next.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(v.getContext(), Main5Activity.class); | |
startActivity(intent); | |
} | |
}); | |
previous = (Button)findViewById(R.id.previous); | |
previous.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(v.getContext(), Main3Activity.class); | |
startActivity(intent); | |
} | |
}); | |
} | |
@Override | |
public void onClick (View v){ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
public class Main5Activity extends AppCompatActivity implements View.OnClickListener { | |
Button next; | |
Button previous; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main5); | |
next = (Button)findViewById(R.id.next); | |
next.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(v.getContext(), MainActivityDate.class); | |
startActivity(intent); | |
} | |
}); | |
previous = (Button)findViewById(R.id.previous); | |
previous.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(v.getContext(), Main4Activity.class); | |
startActivity(intent); | |
} | |
}); | |
} | |
@Override | |
public void onClick (View v){ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.support.v4.view.ViewPager; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.AdapterView; | |
import android.widget.BaseAdapter; | |
import android.widget.Button; | |
import android.widget.Gallery; | |
import android.widget.ImageView; | |
import java.util.List; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
public class MainActivity extends AppCompatActivity implements View.OnClickListener { | |
Button next; | |
Button bpg; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
next = (Button) findViewById(R.id.next); | |
next.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(v.getContext(), Main2Activity.class); | |
startActivity(intent); | |
} | |
}); | |
bpg = (Button) findViewById(R.id.bpg); | |
bpg.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(v.getContext(), MainActivityPhoto.class); | |
startActivity(intent); | |
} | |
}); | |
} | |
@Override | |
public void onClick(View v) { | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.v4.app.DialogFragment; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.DatePicker; | |
import android.widget.Toast; | |
public class MainActivityDate extends AppCompatActivity { | |
DatePicker simpleDatePicker; | |
Button submit; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main_date); | |
final DatePicker simpleDatePicker = (DatePicker) findViewById(R.id.simpleDatePicker); | |
submit = (Button) findViewById(R.id.submitButton); | |
submit.setOnClickListener(new View.OnClickListener(){ | |
@Override | |
public void onClick(View v) { | |
String confirmation = "You have successfully set the next exam on:" +"\r"+ simpleDatePicker.getDayOfMonth()+ "/"+ (simpleDatePicker.getMonth()+ 1)+"/"+ simpleDatePicker.getYear(); | |
Toast.makeText(getApplicationContext(),confirmation, Toast.LENGTH_LONG).show(); | |
Intent intent = new Intent(v.getContext(), MainActivityForm.class); | |
startActivity(intent); | |
} | |
}); | |
int day = simpleDatePicker.getDayOfMonth(); | |
int month = simpleDatePicker.getMonth(); | |
int year = simpleDatePicker.getYear(); | |
int firstDay = simpleDatePicker.getFirstDayOfWeek(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
import android.app.ListActivity; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.support.v7.widget.SearchView; | |
import android.view.Menu; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.ListAdapter; | |
import android.widget.ListView; | |
import android.widget.SimpleAdapter; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
public class MainActivityDB extends ListActivity implements android.view.View.OnClickListener{ | |
Button btnExit; | |
Button btnAdd,btnGetAll; | |
TextView student_Id; | |
@Override | |
public void onClick(View view) { | |
if (view== findViewById(R.id.btnAdd)){ | |
Intent intent = new Intent(this,StudentDetail.class); | |
intent.putExtra("student_Id",0); | |
startActivity(intent); | |
}else { | |
StudentRepo repo = new StudentRepo(this); | |
ArrayList<HashMap<String, String>> studentList = repo.getStudentList(); | |
if(studentList.size()!=0) { | |
ListView lv = getListView(); | |
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
student_Id = (TextView) view.findViewById(R.id.student_Id); | |
String studentId = student_Id.getText().toString(); | |
Intent objIndent = new Intent(getApplicationContext(),StudentDetail.class); | |
objIndent.putExtra("student_Id", Integer.parseInt( studentId)); | |
startActivity(objIndent); | |
} | |
}); | |
ListAdapter adapter = new SimpleAdapter( MainActivityDB.this,studentList, R.layout.view_student_entry, new String[] { "id","name"}, new int[] {R.id.student_Id, R.id.student_name}); | |
setListAdapter(adapter); | |
}else{ | |
Toast.makeText(this,"No student!",Toast.LENGTH_SHORT).show(); | |
} | |
} | |
btnExit = (Button)findViewById(R.id.btnExit); | |
btnExit.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(v.getContext(), ExitDialog.class); | |
startActivity(intent); | |
} | |
}); | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main_db); | |
btnAdd = (Button) findViewById(R.id.btnAdd); | |
btnAdd.setOnClickListener(this); | |
btnGetAll = (Button) findViewById(R.id.btnGetAll); | |
btnGetAll.setOnClickListener(this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.LinearLayout; | |
import com.example.popescu.quizapp.R; | |
public class MainActivityForm extends AppCompatActivity { | |
Button dtb; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main_form); | |
dtb = (Button)findViewById(R.id.dtb); | |
dtb.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(v.getContext(), MainActivityDB.class); | |
startActivity(intent); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
import android.support.v4.view.ViewPager; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
public class MainActivityPhoto extends AppCompatActivity { | |
ViewPager viewPager; | |
CustomSwipeAdapter adapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main_photo); | |
viewPager = (ViewPager) findViewById(R.id.view_pager); | |
adapter = new CustomSwipeAdapter(this); | |
viewPager.setAdapter(adapter); | |
Timer tm = new Timer(); | |
tm.scheduleAtFixedRate(new MyTimer(),2000,2000); | |
} | |
public class MyTimer extends TimerTask { | |
@Override | |
public void run(){ | |
MainActivityPhoto.this.runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
if(viewPager.getCurrentItem()==0){ | |
viewPager.setCurrentItem(1); | |
} | |
else if(viewPager.getCurrentItem()==1) { | |
viewPager.setCurrentItem(2); | |
} | |
else if(viewPager.getCurrentItem()==2) { | |
viewPager.setCurrentItem(3); | |
} | |
else if(viewPager.getCurrentItem()==3){ | |
viewPager.setCurrentItem(4); | |
}else | |
{ | |
viewPager.setCurrentItem(0); | |
} | |
} | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
/** | |
* Created by POPESCU on 3/26/2017. | |
*/ | |
public class Student { | |
// Labels table name | |
public static final String TABLE = "Student"; | |
// Labels Table Columns names | |
public static final String KEY_ID = "id"; | |
public static final String KEY_name = "name"; | |
public static final String KEY_email = "email"; | |
public static final String KEY_age = "age"; | |
public static final String KEY_cnp= "cnp"; | |
// property help us to keep data | |
public int student_ID; | |
public String name; | |
public String email; | |
public int age; | |
public long cnp; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
public class StudentDetail extends AppCompatActivity implements android.view.View.OnClickListener{ | |
Button btnSave , btnDelete; | |
Button btnClose; | |
EditText editTextName; | |
EditText editTextEmail; | |
EditText editTextAge; | |
EditText editTextCnp; | |
private int _Student_Id=0; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_student_detail2); | |
btnSave = (Button) findViewById(R.id.btnSave); | |
btnDelete = (Button) findViewById(R.id.btnDelete); | |
btnClose = (Button) findViewById(R.id.btnClose); | |
editTextName = (EditText) findViewById(R.id.editTextName); | |
editTextEmail = (EditText) findViewById(R.id.editTextEmail); | |
editTextAge = (EditText) findViewById(R.id.editTextAge); | |
editTextCnp = (EditText) findViewById(R.id.editTextCnp); | |
btnSave.setOnClickListener(this); | |
btnDelete.setOnClickListener(this); | |
btnClose.setOnClickListener(this); | |
_Student_Id =0; | |
Intent intent = getIntent(); | |
_Student_Id =intent.getIntExtra("student_Id", 0); | |
StudentRepo repo = new StudentRepo(this); | |
Student student = new Student(); | |
student = repo.getStudentById(_Student_Id); | |
editTextAge.setText(String.valueOf(student.age)); | |
editTextName.setText(student.name); | |
editTextEmail.setText(student.email); | |
editTextCnp.setText(String.valueOf(student.cnp)); | |
} | |
public void onClick(View view) { | |
if (view == findViewById(R.id.btnSave)){ | |
StudentRepo repo = new StudentRepo(this); | |
Student student = new Student(); | |
student.age= Integer.parseInt(editTextAge.getText().toString()); | |
student.cnp= Long.parseLong(editTextCnp.getText().toString()); | |
student.email=editTextEmail.getText().toString(); | |
student.name=editTextName.getText().toString(); | |
student.student_ID=_Student_Id; | |
if (_Student_Id==0){ | |
_Student_Id = repo.insert(student); | |
Toast.makeText(this,"New Student Insert",Toast.LENGTH_SHORT).show(); | |
}else{ | |
repo.update(student); | |
Toast.makeText(this,"Student Record updated",Toast.LENGTH_SHORT).show(); | |
} | |
}else if (view== findViewById(R.id.btnDelete)){ | |
StudentRepo repo = new StudentRepo(this); | |
repo.delete(_Student_Id); | |
Toast.makeText(this, "Student Record Deleted", Toast.LENGTH_SHORT); | |
finish(); | |
}else if (view== findViewById(R.id.btnClose)){ | |
finish(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.popescu.quizapp; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
/** | |
* Created by POPESCU on 3/26/2017. | |
*/ | |
public class StudentRepo { | |
private DBHelper dbHelper; | |
public StudentRepo(Context context) { | |
dbHelper = new DBHelper(context); | |
} | |
public int insert(Student student) { | |
//Open connection to write data | |
SQLiteDatabase db = dbHelper.getWritableDatabase(); | |
ContentValues values = new ContentValues(); | |
values.put(Student.KEY_age, student.age); | |
values.put(Student.KEY_cnp, student.cnp); | |
values.put(Student.KEY_email,student.email); | |
values.put(Student.KEY_name, student.name); | |
// Inserting Row | |
long student_Id = db.insert(Student.TABLE, null, values); | |
db.close(); // Closing database connection | |
return (int) student_Id; | |
} | |
public void delete(int student_Id) { | |
SQLiteDatabase db = dbHelper.getWritableDatabase(); | |
// It's a good practice to use parameter ?, instead of concatenate string | |
db.delete(Student.TABLE, Student.KEY_ID + "= ?", new String[] { String.valueOf(student_Id) }); | |
db.close(); // Closing database connection | |
} | |
public void update(Student student) { | |
SQLiteDatabase db = dbHelper.getWritableDatabase(); | |
ContentValues values = new ContentValues(); | |
values.put(Student.KEY_age, student.age); | |
values.put(Student.KEY_email,student.email); | |
values.put(Student.KEY_name, student.name); | |
values.put(Student.KEY_cnp, student.cnp); | |
// It's a good practice to use parameter ?, instead of concatenate string | |
db.update(Student.TABLE, values, Student.KEY_ID + "= ?", new String[] { String.valueOf(student.student_ID) }); | |
db.close(); // Closing database connection | |
} | |
public ArrayList<HashMap<String, String>> getStudentList() { | |
//Open connection to read only | |
SQLiteDatabase db = dbHelper.getReadableDatabase(); | |
String selectQuery = "SELECT " + | |
Student.KEY_ID + "," + | |
Student.KEY_name + "," + | |
Student.KEY_cnp + "," + | |
Student.KEY_email + "," + | |
Student.KEY_age + | |
" FROM " + Student.TABLE; | |
//Student student = new Student(); | |
ArrayList<HashMap<String, String>> studentList = new ArrayList<HashMap<String, String>>(); | |
Cursor cursor = db.rawQuery(selectQuery, null); | |
// looping through all rows and adding to list | |
if (cursor.moveToFirst()) { | |
do { | |
HashMap<String, String> student = new HashMap<String, String>(); | |
student.put("id", cursor.getString(cursor.getColumnIndex(Student.KEY_ID))); | |
student.put("name", cursor.getString(cursor.getColumnIndex(Student.KEY_name))); | |
studentList.add(student); | |
} while (cursor.moveToNext()); | |
} | |
cursor.close(); | |
db.close(); | |
return studentList; | |
} | |
public Student getStudentById(int Id){ | |
SQLiteDatabase db = dbHelper.getReadableDatabase(); | |
String selectQuery = "SELECT " + | |
Student.KEY_ID + "," + | |
Student.KEY_name + "," + | |
Student.KEY_email + "," + | |
Student.KEY_age + "," + | |
Student.KEY_cnp + | |
" FROM " + Student.TABLE | |
+ " WHERE " + | |
Student.KEY_ID + "=?";// It's a good practice to use parameter ?, instead of concatenate string | |
int iCount =0; | |
Student student = new Student(); | |
Cursor cursor = db.rawQuery(selectQuery, new String[] { String.valueOf(Id) } ); | |
if (cursor.moveToFirst()) { | |
do { | |
student.student_ID =cursor.getInt(cursor.getColumnIndex(Student.KEY_ID)); | |
student.name =cursor.getString(cursor.getColumnIndex(Student.KEY_name)); | |
student.email =cursor.getString(cursor.getColumnIndex(Student.KEY_email)); | |
student.age =cursor.getInt(cursor.getColumnIndex(Student.KEY_age)); | |
student.cnp =cursor.getInt(cursor.getColumnIndex(Student.KEY_cnp)); | |
} while (cursor.moveToNext()); | |
} | |
cursor.close(); | |
db.close(); | |
return student; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment