如何单击ListView项并打开引用我单击的项的活动?

时间:2018-12-28 02:11:35

标签: java android mobile

我单击一个自定义列表视图项目,每个人都打开相同的活动,我希望每个项目都打开一个不同的活动。 我已经尝试了几种方法,但是我没有理解,我是一个初学者,并且我没有太多的知识,所以我来这里寻求帮助。 不一定要准备好代码,您可以告诉我一些帮助,这会有所帮助

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

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

    CustomAdapter customAdapter = new CustomAdapter();

    listview.setAdapter(customAdapter);

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



}

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

}

1 个答案:

答案 0 :(得分:0)

您可以使用switch语句。例如onItemClick内:

switch (position) {
case 0: 
//start Activity 1
break;
case 1:
// start Activity 2
break;
// etc
}

您没有告诉我们这些活动是什么,但是总的原则是使用尽可能少的活动。也许您可以使用一个片段,然后从onItemClick更改该片段的一些数据。