Aprendendo Sqlite
Estou tentando setar um texto com o último valor salvo no SQLite. Vocês sabem como fazer isso .
.
.
package com.testesqlite.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class BD {
private SQLiteDatabase bd;
public BD(Context context){
BDCore auxBd = new BDCore(context);
bd = auxBd.getWritableDatabase();
}
public void inserir(Usuario usuario){
ContentValues valores = new ContentValues();
valores.put("nome", usuario.getNome());
bd.insert("usuario", null, valores);
}
public void atualizar(Usuario usuario){
ContentValues valores = new ContentValues();
valores.put("nome", usuario.getNome());
bd.update("usuario", valores, "_id = ?", new String[]{""+usuario.getId()});
}
public void deletar(Usuario usuario){
bd.delete("usuario", "_id = "+usuario.getId(), null);
}
}
package com.testesqlite.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class BDCore extends SQLiteOpenHelper {
private static final String NOME_BD = “teste”;
private static final int VERSAO_BD = 1;
public BDCore(Context ctx){
super(ctx, NOME_BD, null, VERSAO_BD);
}
@Override
public void onCreate(SQLiteDatabase bd) {
bd.execSQL("create table usuario(_id integer primary key autoincrement, nome text not null);");
}
@Override
public void onUpgrade(SQLiteDatabase bd, int arg1, int arg2) {
bd.execSQL("drop table usuario;");
onCreate(bd);
}
}
package com.testesqlite.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.testesqlite.myapplication.R;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends Activity {
private Usuario usuario = new Usuario();
EditText edittexto;
TextView texto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittexto = (EditText) findViewById(R.id.edittexto);
texto = (TextView) findViewById(R.id.texto);
BD bd = new BD(this);
texto.setText(usuario.getNome());
}
public void Salvar(View view) {
edittexto = (EditText) findViewById(R.id.edittexto);
usuario.setNome(edittexto.getText().toString());
BD bd = new BD(this);
bd.inserir(usuario);
Toast.makeText(this, "Usuário inserido com sucesso!", Toast.LENGTH_SHORT).show();
}
}