Open Activity by clicking an item in a custom listview

Asked

Viewed 35 times

0

I created a custom listview, it’s all right, I 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, I’m a beginner, I wanted a help :/

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 below, but all the items on the list open the same...

  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);
        }
    });
  • You would need a method to carry out the action, so I saw you have no.

  • After listview.setAdapter(customAdapter);

  • Could you post your xml please?

  • Put a breakpoint on the line it.putExtra("a1",position) to see if the position is really counting, sometimes you need to create an auxiliary variable for this.

  • Keeping up because I have a similar problem.

  • Laura, I later made a custom listview, and tried it the same way, when I clicked on an open the same thing, I decided using the case.

Show 1 more comment

1 answer

1

got it this way

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

Browser other questions tagged

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