0
How to condition or check an item in a listview?
I have an Array with some names, I would like to identify the name clicked so I could make a condition, for example I clicked on "Andrew Murray", then from that item it will pull information from Andrew Murray in another Array.
How is it possible to do this?
My code:
// CRIANDO O ARRAY
final String[] autores =
{
"A W Pink",
"A W Tozer",
"Abigail Van Buren",
"Abraham Kuyper",
"Adoniran Judson",
"Agostinho",
"Alexander Peden",
"Allan Redpath",
"Alvin Reid",
"Andrew Bonar",
"Andrew Murray",
"Andrew Young"
};
ArrayAdapter<String> adaptador = new ArrayAdapter<String>
(
// Primeiro Parametro do Array Adpater é o Context
getApplicationContext(),
// Segundo Parametro do Array Adpater é o Layout
android.R.layout.simple_list_item_1,
android.R.id.text1,
// Terceiro Parametro do Array Adapter é indicar o nome do Array para exibição
autores
);
lista.setAdapter(adaptador);
// EVENTO DE CLIQUE
lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent = new Intent(Titulo.this,Autor.class);
startActivity(intent);
}
});
Just one remark to compare
strings
the method==
does not work very well (it is a reference comparison, ie both objects point to the same memory location), for this the best option is to use the.equals
(evaluates the comparison of values in objects) type:"Andrew Murray".equals(autores[position])
– brow-joe
Beauty @Brow-joe !
– viana
Thank you very much, solved the problem! At first you would have to add all authors in the condition, the only thing that could improve would be using sqlite. Valeu.
– Tisco