2
I have a simple phone book system where there’s Activity
s to:
- Register;
- Edition of the register;
- Removal of the register;
- And one last to see all contacts within one
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();
}
}
You need to create a
Listener
. http://stackoverflow.com/questions/10295226/how-to-create-listview-onitemclicklistener– Skywalker
Hi Lucas Moresco, thanks for your help, but I don’t understand why my list is wrong in this passage: list.setOnItemClickListener(new Adapterview.Onitemclicklistener() { @Override public void onItemClick(Adapterview<?> arg0, View arg1, int arg2, long Arg3) { Log.d("##","Items " + list[arg2] ); } }); }
– Leonardo Rocha