Skip to content

Instantly share code, notes, and snippets.

@rafaeltoledo
Created November 7, 2019 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaeltoledo/7778da5260650877a4da35762b15f16c to your computer and use it in GitHub Desktop.
Save rafaeltoledo/7778da5260650877a4da35762b15f16c to your computer and use it in GitHub Desktop.
Android 25
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE restaurantes (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
" nome TEXT, endereco TEXT, tipo TEXT, anotacoes TEXT, twitter TEXT," +
" latitude REAL, longitude REAL, telefone TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2) {
db.execSQL("ALTER TABLE restaurantes ADD COLUMN twitter TEXT");
}
if (oldVersion < 3) {
db.execSQL("ALTER TABLE restaurantes ADD COLUMN latitude REAL");
db.execSQL("ALTER TABLE restaurantes ADD COLUMN longitude REAL");
}
if (oldVersion < 4) {
db.execSQL("ALTER TABLE restaurantes ADD COLUMN telefone TEXT");
}
}
public void inserir(String nome, String endereco, String tipo, String anotacoes, String twitter,
String telefone) {
ContentValues valores = new ContentValues();
valores.put("nome", nome);
valores.put("endereco", endereco);
valores.put("tipo", tipo);
valores.put("anotacoes", anotacoes);
valores.put("twitter", twitter);
valores.put("telefone", telefone);
getWritableDatabase().insert("restaurantes", "nome", valores);
}
public void atualizar(String id, String nome, String endereco, String tipo, String anotacoes,
String twitter, String telefone) {
ContentValues valores = new ContentValues();
String[] argumentos = {id};
valores.put("nome", nome);
valores.put("endereco", endereco);
valores.put("tipo", tipo);
valores.put("anotacoes", anotacoes);
valores.put("twitter", twitter);
valores.put("telefone", telefone);
getWritableDatabase().update("restaurantes", valores, "_id=?", argumentos);
}
public Cursor obterTodos(String ordenacao) {
return getReadableDatabase().rawQuery("select _id, nome, endereco, tipo, " +
"anotacoes, twitter, latitude, longitude, telefone FROM restaurantes ORDER BY " +
ordenacao, null);
}
public Cursor obterPorId(String id) {
String[] argumentos = {id};
return getReadableDatabase().rawQuery(
"SELECT _id, nome, endereco, tipo, anotacoes, twitter, latitude," +
" longitude, telefone FROM restaurantes WHERE _id = ?", argumentos);
}
public String obterTelefone(Cursor c) {
return c.getString(8);
}
<TableRow>
<TextView android:text="@string/telefone" />
<EditText android:id="@+id/telefone" />
</TableRow>
<TableRow>
<TextView android:text="@string/telefone" />
<EditText
android:id="@+id/telefone"
android:layout_span="2" />
</TableRow>
EditText telefone = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form_detalhes);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
gerenciador = new GerenciadorRestaurantes(this);
nome = (EditText) findViewById(R.id.nome);
endereco = (EditText) findViewById(R.id.end);
telefone = (EditText) findViewById(R.id.telefone);
anotacoes = (EditText) findViewById(R.id.anotacoes);
twitter = (EditText) findViewById(R.id.twitter);
tipos = (RadioGroup) findViewById(R.id.tipos);
localizacao = (TextView) findViewById(R.id.localizacao);
idRestaurante = getIntent().getStringExtra(ListaRestaurantes._ID);
if (idRestaurante != null) {
carregar();
}
}
private void salvar() {
String tipo = null;
switch (tipos.getCheckedRadioButtonId()) {
case R.id.rodizio:
tipo = "rodizio";
break;
case R.id.fast_food:
tipo = "fast_food";
break;
case R.id.a_domicilio:
tipo = "a_domicilio";
break;
}
if (tipo != null && endereco.getText().toString() != null &&
nome.getText().toString() != null) {
if (idRestaurante == null) {
gerenciador.inserir(nome.getText().toString(),
endereco.getText().toString(),
tipo, anotacoes.getText().toString(),
twitter.getText().toString(), telefone.getText().toString());
} else {
gerenciador.atualizar(idRestaurante,
nome.getText().toString(),
endereco.getText().toString(),
tipo, anotacoes.getText().toString(),
twitter.getText().toString(), telefone.getText().toString());
}
}
finish();
}
private void carregar() {
Cursor c = gerenciador.obterPorId(idRestaurante);
c.moveToFirst();
nome.setText(gerenciador.obterNome(c));
endereco.setText(gerenciador.obterEndereco(c));
telefone.setText(gerenciador.obterTelefone(c));
anotacoes.setText(gerenciador.obterAnotacoes(c));
twitter.setText(gerenciador.obterTwitter(c));
if (gerenciador.obterTipo(c).equals("rodizio")) {
tipos.check(R.id.rodizio);
} else if (gerenciador.obterTipo(c).equals("fast_food")) {
tipos.check(R.id.fast_food);
} else {
tipos.check(R.id.a_domicilio);
}
latitude = gerenciador.obterLatitude(c);
longitude = gerenciador.obterLongitude(c);
localizacao.setText(String.valueOf(gerenciador.obterLatitude(c)) +
", " + String.valueOf(gerenciador.obterLongitude(c)));
c.close();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("nome", nome.getText().toString());
outState.putString("endereco", endereco.getText().toString());
outState.putString("telefone", telefone.getText().toString());
outState.putString("anotacoes", anotacoes.getText().toString());
outState.putInt("tipo", tipos.getCheckedRadioButtonId());
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
nome.setText(savedInstanceState.getString("nome"));
endereco.setText(savedInstanceState.getString("endereco"));
telefone.setText(savedInstanceState.getString("telefone"));
anotacoes.setText(savedInstanceState.getString("anotacoes"));
tipos.check(savedInstanceState.getInt("tipo"));
}
<uses-permission android:name="android.permission.CALL_PHONE" />
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/twitter"
android:title="@string/timeline_twitter"
android:icon="@drawable/twitter" />
<item
android:id="@+id/localizacao"
android:title="@string/salvar_localizacao"
android:icon="@drawable/gps" />
<item
android:id="@+id/mapa"
android:title="@string/exibir_mapa"
android:icon="@drawable/mapa" />
<item
android:id="@+id/chamar"
android:title="@string/chamar"
android:icon="@drawable/chamada" />
</menu>
<string name="telefone">Telefone:</string>
<string name="chamar">Telefonar</string>
<string name="telefone">Teléfono:</string>
<string name="chamar">Llamar</string>
} else if (item.getItemId() == R.id.chamar) {
String numero = "tel:" + telefone.getText().toString();
if (numero.length() > 4) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(numero)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment