Open an Activity by clicking an item in a list view

Asked

Viewed 456 times

0

I’m a little lost already, I want that when click on an item in the list it opens another Activity, but only with Toast... I’m beginner so if you can help me thank.

public class estatisticas extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_estatisticas);
    int position = getIntent().getIntExtra("a", 1);

    final ListView lista =  findViewById(R.id.Listview);
    final ArrayList<String> estatisticas = preencherdados();

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, estatisticas);
    lista.setAdapter(arrayAdapter);

    lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            estatisticas artilheiros = (estatisticas) lista.getItemAtPosition(1);
            Intent it = new Intent(estatisticas.this, artilheiros.class);
            startActivity(it);
            it.putExtra("a", position = 1);
        }
    });


}

private ArrayList<String> preencherdados() {
    ArrayList<String> dados = new ArrayList<String>();

    dados.add("TOP 15 ARTILHEIROS");
    dados.add("Campeões");
    return dados;
}

}

2 answers

0

I must assume that your intention was to pass the position by Intent, but you are starting Activity before passing the data by Intent, the correct is to call putExtra() before starting Activity, this should solve:

lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent it = new Intent(estatisticas.this, artilheiros.class);
            it.putExtra("a", position);
            startActivity(it);
        }
    });

A good practice is not to create classes that start with lowercase letters as described by the oracle and the name should be the same as your file . java, this helps maintain the readability of your source code.

Edited

If you want to open an Activity for the clicked item then just use position which returns the position of the item in the list, when you assign the value 1 in position = 1 will always be the item that is at position 1 of the list.

  • I got it, but if my listview has 2 items or more, when I click on the 2 the same thing, I wanted it to be 1 for each, you know? :/

  • @vitu try now

  • I didn’t get it right, can you explain it to me again? would you return the "position"? that I tried and it didn’t work, maybe I tried wrong

  • @vitu position returns is the position of the item you clicked on the list, example if your list has 3 elements and you click on the second it will return 1, if you click on the first 0, if on the third 2, the problem is that you are assigning 1 in putExtra, ie it will always return to the second position of the list

-1

mListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            //aqui vc irá fazer a acao com o click pegando uma posicao da sua lista, coloquei um exemplo de iniciar uma nova tela passando o item clicado por parametro.
            Intent intent = new Intent(this, MinhaProximaTela.class);
            intent.putExtra("mParametro", mParametro[position]);
            startActivity(intent);
        }
    });

Browser other questions tagged

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