How can I condition or check an item in a listview?

0

How can I 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 can you 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);
            }
        });
    
asked by anonymous 25.05.2017 / 14:34

2 answers

0

If you want to know which item the user has clicked, simply use the position relative to the click on your strings vector . See:

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, however it can be a bit costly since you would have to add all the 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 are located, so that you can adapt that answer.

    
25.05.2017 / 15:17
0

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

You can do this as follows:

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 name of the author in the variable autor , based on this you can already make the appropriate treatments

    
25.05.2017 / 15:19