How is it possible to condition or check an item in a listview?

Asked

Viewed 53 times

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);
            }
        });

2 answers

0


If you want to know which item the user clicked on, just use the position referring to the click on your vector strings. Behold:

lista.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {

    // Toast mostrando qual possição e o autor clicado
    Toast.makeText(getApplicationContext(),
      "Posição: " + position+" Autor:"+autores[position], Toast.LENGTH_LONG)
      .show();

     if("Andrew Murray".equals(autores[position])){
         // se entrar aqui nesta condição, quer dizer que o usuário clicou em
         // Andrew Murray
     }
  }
}); 

This already answers your question, but it can be a little costly as it would have to add all authors in the condition. To improve the solution, you would have to add more details of your problem, for example, where the details of each author is located, so that it is possible to adapt this answer.

  • 1

    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])

  • Beauty @Brow-joe !

  • 2

    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.

0

To identify the item clicked inside the listview you will need to use the method setOnItemClickListener, in it there is an integer parameter called position, on it is the position of the list that was clicked, basically you will only need to do autores[position]

you can do it this way:

lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String autor = autores[position];
        Toast.makeText(context, "Você Clicou em: " + autor, Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Titulo.this,Autor.class);
            startActivity(intent);
    }
});

this way you already have the author name in the variable autor, based on this you can already make the appropriate treatments

Browser other questions tagged

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