I have a simple phonebook system, where there is Activity
s for:
- Registration;
- Editing the registry;
- Removing the registration;
- And one last to see all contacts within a
ListView
.
All records come from a table in the database. I would like to know how to access each record when tapping a record line in the list.
package com.br.projetoeducadastro;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class Contatos extends Activity {
private SQLiteDatabase db;
private CursorAdapter dataSource;
private static final String campos[] = {"_id", "nome", "telefone"};
ListView list;
SQLiteHelper helper;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Contatos.this, android.R.layout.simple_list_item_1);
TextView tvNome, tvId, tvTelefone;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contatos);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
helper = new SQLiteHelper(this);
db = helper.getWritableDatabase();
Cursor c = db.query("clientes", campos, null, null, null, null, null);
if (c.getCount() > 0) {
dataSource = new SimpleCursorAdapter(this, R.layout.row, c, campos,
new int[]{R.id.tvId, R.id.tvNome, R.id.tvTelefone});
list.setAdapter(dataSource);
} else {
Toast.makeText(this, "Nenhum Registro Encontrado", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Contatos.this, Index.class);
startActivity(intent);
}
}
@Override
public void onDestroy() {
super.onDestroy();
db.close();
}
}