Open activity when clicking on an item in a custom listview

0

I created a custom listview, it's all right, I've tried it here but it's not right, I wanted to implement the click on an item, but since it's custom listview maybe it's something different,

public class artilheiros extends AppCompatActivity {

int [] Imagens = {R.drawable.arg, R.drawable.bolivia, R.drawable.brasil, R.drawable.chile,
          R.drawable.colombia, R.drawable.chile, R.drawable.paraguai, R.drawable.uruguai,
          R.drawable.arg, R.drawable.bolivia, R.drawable.brasil, R.drawable.chile,
          R.drawable.colombia, R.drawable.chile, R.drawable.paraguai};

String [] Nomes = {"ART 1", "ART 2", "ART 3", "ART 4", "ART 5", "ART 6", "ART 7", "ART 8",
                    "ART 9", "ART 10", "ART 11", "ART 12", "ART 13", "ART 14", "ART 15"};

String [] Times = {"TIME 1", "TIME 2", "TIME 3", "TIME 4", "TIME 5", "TIME 6", "TIME 7",
        "TIME 8", "TIME 9", "TIME 10", "TIME 11", "TIME 12", "TIME 13", "TIME 14", "TIME 15"};

String [] Gols = {"999 gols", "999 gols", "999 gols", "999 gols", "999 gols", "999 gols", "999 gols",
       "999 gols", "999 gols", "999 gols", "999 gols", "999 gols", "999 gols", "999 gols", "999 gols"};



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_artilheiros);

    ListView listview = findViewById(R.id.listviewart);

    CustomAdapter customAdapter = new CustomAdapter();

    listview.setAdapter(customAdapter);

}

class CustomAdapter extends BaseAdapter{


    @Override
    public int getCount() {
        return Imagens.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        convertView = getLayoutInflater().inflate(R.layout.customlistview, null);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
        TextView textViewnome = (TextView) convertView.findViewById(R.id.textViewnome);
        TextView textViewtime = (TextView) convertView.findViewById(R.id.textViewtime);
        TextView textViewgols = (TextView) convertView.findViewById(R.id.textViewgols);

        imageView.setImageResource(Imagens[position]);
        textViewnome.setText(Nomes[position]);
        textViewtime.setText(Times[position]);
       textViewgols.setText(Gols[position]);


        return convertView;
    }
}

}

I used this method, but all the items in the list open the same activity ...

  int position = getIntent().getIntExtra("a", 0);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            artilheiros a1 = (artilheiros) listview.getItemAtPosition(0);
            Intent it = new Intent(artilheiros.this, campeoes.class);
            it.putExtra("a1", position);
            startActivity(it);
        }
    });
    
asked by anonymous 27.12.2018 / 21:36

1 answer

1

I got it so

   switch (position) {
                case 0:
                    campeoes a1 = (campeoes) listview.getItemAtPosition(0);
                    Intent it = new Intent(campeoes.this, campeoes.class);
                    it.putExtra("a1", position);
                    startActivity(it);

                    break;
                case 1:
                    campeoes a2 = (campeoes) listview.getItemAtPosition(0);
                    Intent it2 = new Intent(campeoes.this, estatisticas.class);
                    it2.putExtra("a2", position);
                    startActivity(it2);
                    break;
                case 2:
                    campeoes a3 = (campeoes) listview.getItemAtPosition(0);
                    Intent it3 = new Intent(campeoes.this, estatisticas.class);
                    it3.putExtra("a3", position);
                    startActivity(it3);
                    break;
            }
    
29.12.2018 / 01:09