Reputation: 544
I have a ListView with Contact info(name, phone number) so i want when i click in the contact name i want to show its name and phone number in a dialog box (which a have a code for it already) which is:
public void ShowMessage(String titulo,String msg){
AlertDialog.Builder dialogo = new AlertDialog.Builder(this);
dialogo.setMessage(msg);
dialogo.setTitle(titulo);
dialogo.setNeutralButton("OK", null);
dialogo.show();
}
Then i have seen about the setOnItemClickListener
but when i try to put this up in my .java file it doens't even suggest the code, does anyone know why or how to do it?
EDIT:
//LISTVIEW database CONTATO
ListView user = (ListView) findViewById(R.id.lvShowContatos);
//String = simple value ||| String[] = multiple values/columns
String[] campos = new String[] {"nome", "telefone"};
list = new ArrayList<String>();
Cursor c = db.query( "contatos", campos, null, null, null, null, null);
c.moveToFirst();
String lista = "";
if(c.getCount() > 0) {
while(true) {
list.add(c.getString(c.getColumnIndex("nome")).toString());
if(!c.moveToNext()) break;
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
user.setAdapter(adapter);
that is the code of my listview/adapter
OBS: better if you can explain (no tuts link (better if possible))
Upvotes: 0
Views: 1494
Reputation: 86948
(I see that you are processing a Cursor yourself and using an ArrayAdapter, understand that a SimpleCursorAdapter does this for you. See my note below.)
Anyway, change your Cursor into a class variable and try adding this in onCreate()
:
user.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
c.moveToPosition(position);
String nome = c.getString(c.getColumnIndex("nome"));
String telefone = c.getString(c.getColumnIndex("telefone"));
showMessage(nome, telefone);
}
});
You aren't specific on how the title and message correlate to the contact's name, so I made that part up.
A class variable is simply a variable defined in a place that makes it visible to the entire class. For example this turns c
into a class variable so you can use it in onItemClick()
:
public class MyActivity extends Activity {
Cursor c;
public void onCreate(...) {
...
c = db.query( "contatos", campos, null, null, null, null, "nome");
...
}
}
Understand that you can simplify how you read your contacts:
list = new ArrayList<String>();
Cursor c = db.query("contatos", campos, null, null, null, null, "nome");
int nameIndex = c.getColumnIndex("nome");
while(c.moveToNext()) {
list.add(c.getString(nameIndex));
}
I made a couple changes:
"nome"
column once, it won't change unless you change the Cursor.moveToFirst()
returns true
if there is data to read and false
if not.This is faster to write and faster to run than your existing method.
A SimpleCursorAdapter is the standard adapter to bind data from your Cursor to a ListView. This will give you the same results as your original method, but with much less code.
How to use a SimpleCursorAdapter:
Cursor c = db.query("contatos", campos, null, null, null, null, "nome");
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, c,
new String[] {"nome"}, new int[] {android.R.id.text1});
user.setAdapter(adapter);
Upvotes: 1
Reputation: 284
Assuming the adapter used in your ListView has a custom type (I'll call it ContactInfo), the following should work.
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// Get String provided to this particular row
ContactInfo info = getListView().getAdapter().getItem(position);
// Construct title, message etc from information within info
showMessage("Contact Info", info);
}
});
Upvotes: 0