@Override public int delete(final Uri uri, final String selection, final String[] selectionArgs) { final List<String> pathSegments = uri.getPathSegments(); if (pathSegments.size() != 1) throw new IllegalArgumentException(uri.toString()); final String address = uri.getLastPathSegment(); final int count = helper .getWritableDatabase() .delete(DATABASE_TABLE, KEY_ADDRESS + "=?", new String[] {address}); if (count > 0) getContext().getContentResolver().notifyChange(uri, null); return count; }
@Override public Uri insert(final Uri uri, final ContentValues values) { if (uri.getPathSegments().size() != 1) throw new IllegalArgumentException(uri.toString()); final String address = uri.getLastPathSegment(); values.put(KEY_ADDRESS, address); long rowId = helper.getWritableDatabase().insertOrThrow(DATABASE_TABLE, null, values); final Uri rowUri = contentUri(getContext().getPackageName()) .buildUpon() .appendPath(address) .appendPath(Long.toString(rowId)) .build(); getContext().getContentResolver().notifyChange(rowUri, null); return rowUri; }
public void ukoncitJizdu(View v) { setProbihaJizda(false); // Vyber druhý tab (historie jízd) tabLayout.getTabAt(1).select(); // Příprava databáze Helper h = new Helper(this); SQLiteDatabase rdb = h.getReadableDatabase(); SQLiteDatabase wdb = h.getWritableDatabase(); // Vložit vložit nový záznam s novou jízdou ContentValues cv = new ContentValues(); cv.put("id_auta", id_auta); cv.put("od_misto", ""); cv.put("do_misto", ""); cv.put("od_cas", od_cas); cv.put("do_cas", (int) (System.currentTimeMillis() / 1000L)); cv.put("tankovano", 0); cv.put("plna_nadrz", 0); cv.put("litru", 0); cv.put("soukroma", 0); try { id_jizdy = (int) wdb.insertOrThrow("jizdy", null, cv); } catch (SQLException e) { Log.d("Výjimka", e.getMessage()); } Log.d("idecko", String.valueOf(id_jizdy)); wdb.close(); rdb.close(); // Otevři příslušný detail jízdy DetailJizdyFragment df = new DetailJizdyFragment(); Bundle args = new Bundle(); args.putInt("id_jizdy", id_jizdy); df.setArguments(args); df.show(getSupportFragmentManager(), "detail_jizdy"); }
@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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } if (id == R.id.action_append) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.title_new); builder.setMessage(R.string.message_new); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.edittext, null); builder.setView(view); builder.setNegativeButton(R.string.button_cancel, null); final AlertDialog alertDialog = builder.create(); EditText editText = (EditText) view.findViewById(R.id.editText); editText.setOnKeyListener( new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) { EditText editText = (EditText) v; Editable editable = editText.getText(); ContentValues contentValues = new ContentValues(); contentValues.put("name", editable.toString()); SQLiteDatabase database = helper.getWritableDatabase(); database.insert(tableName, null, contentValues); adapter.changeCursor(helper.getCursor()); alertDialog.dismiss(); return true; } return false; } }); alertDialog.show(); return true; } if (id == R.id.action_delete_all) { // Delete all the records, but do not destroy the database itself. SQLiteDatabase database = helper.getWritableDatabase(); database.delete(tableName, null, null); adapter.changeCursor(helper.getCursor()); return true; } if (id == R.id.action_reset) { // Destroy the database and re-create it. helper.close(); deleteDatabase(databaseName); helper = new Helper(this, databaseName); adapter.changeCursor(helper.getCursor()); return true; } return super.onOptionsItemSelected(item); }
public void open() throws SQLDataException { helper = new Helper(mCtx); myDb = helper.getWritableDatabase(); }