open a new Activity by clicking an item in the listview

Asked

Viewed 77 times

0

I created a listview but I’m not sure which command should I make for my items to open a new activity for each one.

Below follows my code until the moment:

public class pg_apoio extends AppCompatActivity {

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

        ListView listadeapoio = (ListView) findViewById(R.id.listadoapoio);
        ArrayAdapter adapter = new listaapoioadapter(this, adicionarguias());
        listadeapoio.setAdapter(adapter);

        listadeapoio.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(pg_apoio.this, pg_bjcp.class);
                startActivity(intent);
            }
        });

    }



    private ArrayList<listaapoio> adicionarguias() {
        ArrayList<listaapoio> listaapoios = new ArrayList<listaapoio>();

        listaapoio e = new listaapoio ("Guia BJCP 2015", "guia em portugûes",R.drawable.pdf_icon);

        listaapoios.add(e);
        e= new listaapoio("Lupulo", "Guia de lupulos", R.drawable.hop1);

        listaapoios.add(e);
        e= new listaapoio("Levedura", "Guia de leveduras", R.drawable.yeast_icon);

        listaapoios.add(e);
        e= new listaapoio("Harmonização", "Guia de harmonização com cerveja", R.drawable.harmo_icon);

        listaapoios.add(e);
        e= new listaapoio("Taças", "Guia de taças para cada estilo", R.drawable.beerglass_icon);

        listaapoios.add(e);
        e= new listaapoio("Receitas", "Receitas prontas", R.drawable.receita_icon);

        listaapoios.add(e);
        e= new listaapoio("Links" ,"Canais de cervejeiros artesanais", R.drawable.youtubelogo_icon);

        listaapoios.add(e);
        e= new listaapoio("teste", "teste da lista", R.drawable.pdf_icon);

        return listaapoios;

    }

}
  • "items open a new Activity for each" - how so ? This Intent intent = new Intent(pg_apoio.this, pg_bjcp.class); startActivity(intent); already makes each one open in a new Activity.

1 answer

0

In the method, public void onItemClick(Adapterview adapterView, View, int i, long l), it returns the data from the list item you clicked. Example this code piece of code

       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            OrdemServico ordemServico = (OrdemServico) parent.getAdapter().getItem(position)

I’m retrieving the object that is related to the listview item, so I just take some attribute from it. Ex: if(ordemServico.getId = x){ go here } Ex: if(ordemServico.getId = y){ go there}

How will that support list return and

  • William, thank you so much for the tip helped me a lot!

  • Anything, any doubt I can help, just talk

Browser other questions tagged

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