Hello, I'm making a small app and I need to fetch data from the database and display it on the screen.
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String BANCO_DADOS = "Nomes";
private static int VERSION = 1;
public DatabaseHelper(Context context) {
super(context, BANCO_DADOS, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE nomes (_id INTEGER PRIMARY KEY, " +
"nome TEXT, data_nascimento DATE);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
NamesActivity.class
public class NomesActivity extends ListActivity implements AdapterView.OnItemClickListener, DialogInterface.OnClickListener {
private DatabaseHelper helper;
private SimpleDateFormat simpleDateFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tarefas_activity);
String[] de = {"nome" ,"dataNascimento"};
int[] para = {R.id.nome, R.id.dataNascimento};
SimpleAdapter adapter = new SimpleAdapter(this, listarNomes(), R.layout.nomes_list_activity, de, para);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
registerForContextMenu(getListView());
helper = new DatabaseHelper(this);
simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
}
private List<Map<String, Object>> nomes;
private List<Map<String, Object>> listarNomes() {
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT _id, nomes, data_nascimento FROM nomes", null);
cursor.moveToFirst();
nomes = new ArrayList<Map<String, Object>>();
for (int i = 0; i < cursor.getCount(); i++) {
Map<String, Object> item = new HashMap<String, Object>();
String id = cursor.getString(0);
String nome = cursor.getString(1);
long dataNascimento = cursor.getLong(2);
item.put("id", id);
item.put("nome", nome);
Date dataNascimentoDate = new Date(dataNascimento);
String exibirData = "Nasceu em " + simpleDateFormat.format(dataNascimentoDate);
item.put("dataNascimento", exibirData);
nomes.add(item);
cursor.moveToNext();
}
cursor.close();
db.close();
return nomes;
}
}
What happens is that when I enter the Activity, this error appears:
Unfortunately, Names have stopped
I have already looked at the logs and no error message appears. How to solve?