Skip to content

Instantly share code, notes, and snippets.

@lucasemanuel
Created December 5, 2018 13:52
Show Gist options
  • Save lucasemanuel/0ca7086ebccc2352ab436434821111c8 to your computer and use it in GitHub Desktop.
Save lucasemanuel/0ca7086ebccc2352ab436434821111c8 to your computer and use it in GitHub Desktop.
package com.example.emanuel.aluno;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int VERSION = 1;
private static final String DBNAME = "alunos";
public DatabaseHelper(Context context) {
super(context, DBNAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + StudentsEntry.TABLE_NAME +
" ( " + StudentsEntry._ID + " INTERGER PRIMARY KEY AUTOINCREMENT , "
+ StudentsEntry.COLUNM_NAME_NAME + " VARCHAR(255) , "
+ StudentsEntry.COLUNM_NAME_MAT + " VARCHAR(255)) ");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public static class StudentsEntry implements BaseColumns {
public static final String TABLE_NAME = "students";
public static final String COLUNM_NAME_NAME = "name";
public static final String COLUNM_NAME_MAT = "mat";
}
}
package com.example.emanuel.aluno;
import android.database.Cursor;
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.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StudentsTable studentsTable = new StudentsTable(getApplicationContext());
String name = ((EditText)findViewById(R.id.editName)).getText().toString();
String matricula = ((EditText)findViewById(R.id.editMat)).getText().toString();
studentsTable.save(name,matricula);
ListView studentsListView = (ListView) findViewById(R.id.studentsList);
((SimpleCursorAdapter)studentsListView.getAdapter()).changeCursor(studentsTable.list());
}
});
ListView studentsList = (ListView) findViewById(R.id.studentsList);
StudentsTable studentsTable = new StudentsTable(getApplicationContext());
Cursor cursor = studentsTable.list();
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(
this, android.R.layout.simple_expandable_list_item_2, cursor, new String[]{
DatabaseHelper.StudentsEntry.COLUNM_NAME_NAME,
DatabaseHelper.StudentsEntry.COLUNM_NAME_MAT
}, new int[] { android.R.id.text1, android.R.id.text2 }
);
studentsList.setAdapter(simpleCursorAdapter);
}
}
package com.example.emanuel.aluno;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
public class StudentsTable {
DatabaseHelper dbHelper;
public StudentsTable(Context context){
dbHelper = new DatabaseHelper(context);
}
public void save(String name, String matricula){
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.StudentsEntry.COLUNM_NAME_NAME, name);
contentValues.put(DatabaseHelper.StudentsEntry.COLUNM_NAME_MAT, matricula);
long ret = db.insert(DatabaseHelper.StudentsEntry.TABLE_NAME, null, contentValues);
if(ret == -1){
throw new SQLiteException("erro");
}
db.close();
}
public Cursor list(){
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor query = db.query(DatabaseHelper.StudentsEntry.TABLE_NAME, new String[]{
DatabaseHelper.StudentsEntry._ID,
DatabaseHelper.StudentsEntry.COLUNM_NAME_NAME,
DatabaseHelper.StudentsEntry.COLUNM_NAME_NAME
}, null, null, null, null, null);
query.moveToFirst();
db.close();
return query;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment