50
switch (item.getItemId()) {
case R.id.insert:
createNewTask();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
dbHelper.deleteTodo(info.id);
fillData();
return true;
}
return super.onContextItemSelected(item);
}
private void createNewTask() {
Intent intent = new Intent(this, EditActivity.class);
startActivityForResult(intent, ACTIVITY_CREATE);
}
private void fillData() {
cursor = dbHelper.getAllTodos();
startManagingCursor(cursor);
String[] from = new String[] { ToDoDatabase.COLUMN_SUMMARY };
int[] to = new int[] { R.id.label };
String[] from1 = new String[] { ToDoDatabase.COLUMN_CATEGORY };
int[] to1 = new int[] { R.id.kun };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.list_row, cursor, from, to);
SimpleCursorAdapter notes1 = new SimpleCursorAdapter(this,
R.layout.list_row, cursor, from1, to1);
setListAdapter(notes);
setListAdapter(notes1);
}
@Override
protected void onListItemClick(ListView l, View v,
int position, long id) {
super.onListItemClick(l, v,
position, id);
Intent intent = new Intent(this, EditActivity.class);
intent.putExtra(ToDoDatabase.COLUMN_ID, id);
// ushbu metod orqali activity natija
qaytaradi
startActivityForResult(intent, ACTIVITY_EDIT);
}
@Override
protected void onActivityResult(int requestCode,
int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
fillData();
}
51
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (dbHelper != null) {
dbHelper.close();
}
}
}