I'm new to android and I'm working on a scheduling app. I've been using SQLite to store data onto my device and it's been working, however, I do have a delete button that I'm trying to also use to get rid of the item found in the list view.
This is my DatabaseHandler.java code:
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "assignmentManager",
TABLE_ASSIGNMENTS = "assignments",
KEY_ID = "id",
KEY_NAME = "name",
KEY_TIME = "time",
KEY_ASSIGNED = "assigned",
KEY_DUE = "due";
public DatabaseHandler(Context context)
{
super( context, DATABASE_NAME, null, DATABASE_VERSION );
}
@Override
public void onCreate( SQLiteDatabase db )
{
db.execSQL( "CREATE TABLE " + TABLE_ASSIGNMENTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT," + KEY_TIME + " INTEGER," + KEY_ASSIGNED + " TEXT," + KEY_DUE + " TEXT )" );
}
@Override
public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion )
{
db.execSQL( "DROP TABLE IF EXISTS " + TABLE_ASSIGNMENTS );
onCreate( db );
}
public void createAssignment( Assignment assignment )
{
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put( KEY_NAME, assignment.getName() );
values.put( KEY_TIME, assignment.getTime() );
values.put( KEY_ASSIGNED, assignment.getAssigned() );
values.put( KEY_DUE, assignment.getDue() );
db.insert( TABLE_ASSIGNMENTS, null, values );
db.close();
}
public Assignment getAssignment( int id )
{
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query( TABLE_ASSIGNMENTS, new String[] { KEY_ID, KEY_NAME, KEY_TIME, KEY_ASSIGNED, KEY_DUE }, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null );
if ( cursor != null )
cursor.moveToFirst();
GregorianCalendar dateDue = new GregorianCalendar( Assignment.getYear( cursor.getString( 4 ) ), Assignment.getMonth(cursor.getString(4)), Assignment.getDay(cursor.getString(4)) );
GregorianCalendar dateAssigned = new GregorianCalendar( Assignment.getYear( cursor.getString( 3 ) ), Assignment.getMonth( cursor.getString( 3 ) ), Assignment.getDay( cursor.getString( 3 ) ) );
Assignment assignment = new Assignment( Integer.parseInt( cursor.getString( 0 ) ), cursor.getString( 1 ), dateDue, dateAssigned, Integer.parseInt( cursor.getString( 2 ) ) );
db.close();
cursor.close();
return assignment;
}
public void deleteAssignment( Assignment assignment )
{
SQLiteDatabase db = getWritableDatabase();
db.delete( TABLE_ASSIGNMENTS, KEY_ID + "=?", new String[] { String.valueOf( assignment.getId() ) } );
db.close();
}
public int getAssignmentsCount()
{
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery( "SELECT * FROM " + TABLE_ASSIGNMENTS, null );
int count = cursor.getCount();
db.close();
cursor.close();
return count;
}
public int updateAssignment( Assignment assignment )
{
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put( KEY_NAME, assignment.getName() );
values.put( KEY_TIME, assignment.getTime() );
values.put( KEY_ASSIGNED, assignment.getAssigned() );
values.put( KEY_DUE, assignment.getDue() );
return db.update( TABLE_ASSIGNMENTS, values, KEY_ID + "=?", new String[] { String.valueOf( assignment.getId() ) } );
}
public List<Assignment> getAllAssignments()
{
List<Assignment> assignments = new ArrayList<Assignment>();
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery( "SELECT * FROM " + TABLE_ASSIGNMENTS, null );
if ( cursor.moveToFirst() )
{
do {
{
GregorianCalendar dateDue = new GregorianCalendar( Assignment.getYear( cursor.getString( 4 ) ), Assignment.getMonth(cursor.getString(4)), Assignment.getDay(cursor.getString(4)) );
GregorianCalendar dateAssigned = new GregorianCalendar( Assignment.getYear( cursor.getString( 3 ) ), Assignment.getMonth( cursor.getString( 3 ) ), Assignment.getDay( cursor.getString( 3 ) ) );
Assignment assignment = new Assignment( Integer.parseInt( cursor.getString( 0 ) ), cursor.getString( 1 ), dateDue, dateAssigned, Integer.parseInt( cursor.getString( 2 ) ) );
assignments.add( assignment );
}
}
while ( cursor.moveToNext() );
}
return assignments;
}
This is my TeacherActivity.java code:
List<Assignment> a = new ArrayList<Assignment>();
ListView aListView;
DatabaseHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teachers);
TextView teachers = (TextView) findViewById(R.id.teachers);
TextView create = (TextView) findViewById(R.id.create);
TextView assignments = (TextView) findViewById(R.id.assignments);
final EditText assignmentname = (EditText) findViewById(R.id.assignmentname);
final DatePicker due = (DatePicker) findViewById(R.id.due);
final EditText time = (EditText) findViewById(R.id.time);
TabHost tabHost = (TabHost) findViewById(R.id.tabs);
final Button createassignment = (Button) findViewById(R.id.createassignment);
aListView = (ListView) findViewById(R.id.listView);
dbHandler = new DatabaseHandler( getApplicationContext() );
tabHost.setup();
TabHost.TabSpec tabSpec = tabHost.newTabSpec("add");
tabSpec.setContent(R.id.tabAdder);
tabSpec.setIndicator("Add");
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("assignments");
tabSpec.setContent(R.id.tabAssignments);
tabSpec.setIndicator("Assignments");
tabHost.addTab(tabSpec);
Typeface arvo = Typeface.createFromAsset(getAssets(),
"fonts/arvo.otf");
Typeface bebas = Typeface.createFromAsset(getAssets(),
"fonts/bebas.otf");
teachers.setTypeface(arvo);
create.setTypeface(bebas);
assignmentname.setTypeface(bebas);
time.setTypeface(bebas);
createassignment.setTypeface(bebas);
assignments.setTypeface(bebas);
createassignment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GregorianCalendar gd = new GregorianCalendar( due.getYear(), due.getMonth(), due.getDayOfMonth() );
Assignment assignment = new Assignment( dbHandler.getAssignmentsCount(), String.valueOf( assignmentname.getText() ), gd, new GregorianCalendar(), Integer.parseInt(String.valueOf(time.getText())) );
dbHandler.createAssignment( assignment );
a.add( assignment );
populateList();
assignmentname.setText("");
time.setText("");
Toast.makeText(getApplicationContext(), "Assignment created!", Toast.LENGTH_SHORT).show();
}
});
assignmentname.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
createassignment.setEnabled(!assignmentname.getText().toString().trim().isEmpty());
}
@Override
public void afterTextChanged(Editable s) {
}
});
teachers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(TeachersActivity.this, MainActivity.class); // http://stackoverflow.com/a/13194141/2869358
startActivity(i); // http://stackoverflow.com/a/13194141/2869358
}
});
List<Assignment> addableAssignments = dbHandler.getAllAssignments();
int assignmentCount = dbHandler.getAssignmentsCount();
for ( int i = 0; i < assignmentCount; i++ )
{
a.add( addableAssignments.get( i ) );
}
if ( !addableAssignments.isEmpty() )
{
populateList();
}
}
private void populateList()
{
ArrayAdapter<Assignment> adapter = new AssignmentListAdapter();
aListView.setAdapter(adapter);
}
private void addAssignments( String name, GregorianCalendar dateDue, int time )
{
a.add( new Assignment( 0, name, dateDue, new GregorianCalendar(), time ));
Collections.sort(a);
}
private class AssignmentListAdapter extends ArrayAdapter<Assignment>
{
Typeface arvo = Typeface.createFromAsset(getAssets(),
"fonts/arvo.otf");
Typeface bebas = Typeface.createFromAsset(getAssets(),
"fonts/bebas.otf");
public AssignmentListAdapter()
{
super ( TeachersActivity.this, R.layout.listview_assignments, a );
}
@Override
public View getView( final int position, View view, ViewGroup parent )
{
if ( view == null )
{
view = getLayoutInflater().inflate( R.layout.listview_assignments, parent, false );
}
Assignment currentAssignment = a.get( position );
dbHandler = new DatabaseHandler( getApplicationContext() );
TextView name = (TextView) view.findViewById(R.id.name);
TextView due = (TextView) view.findViewById(R.id.due);
TextView time = (TextView) view.findViewById(R.id.time);
TextView assigned = (TextView) view.findViewById(R.id.assigned);
Button delete = (Button) view.findViewById(R.id.delete);
name.setTypeface(bebas);
due.setTypeface(bebas);
time.setTypeface(bebas);
assigned.setTypeface(bebas);
delete.setTypeface(arvo);
name.setText(currentAssignment.getName());
due.setText("Due: " + currentAssignment.getDue());
time.setText("Estimated Time: " + currentAssignment.getTimeString());
assigned.setText("Assigned: " + currentAssignment.getAssigned());
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHandler.deleteAssignment( a.get( position ) );
remove(a.get(position));
notifyDataSetChanged();
}
});
return view;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.teachers, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
The delete button can be found in the getView() method of the TeacherActivity.java class.
I don't know if I'm using the deleteAssignment() method correctly.
Please help!