Sqlite and Android database

Asked

Viewed 185 times

1

Hello, I’m making a small app and need to search database data and display them 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) {

    }
}

Nomesactivity.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 Activity, this error appears:

Unfortunately, Names has stopped

I have already looked at the logs and no error message appears to me. How to resolve?

  • Nothing in the logcat? Just by code I noticed that you try to use the helper in listarNomes() before starting it. Place the line you do the initialization before the Simpleadapter. Other than that, we would need more details of the error.

  • 1

    It appears that there are errors in: String exibirData = "Nasceu em " + simpleDateFormat.format(dataNascimentoDate); and in SimpleAdapter adapter

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.