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;
}
}
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
@vitu try now
– Samuel Ives
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
@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
– Samuel Ives