How to click items from a list in Android Studio?

Asked

Viewed 528 times

0

For now my code is like this ? how do I implement the click of the list items the click would open a new screen

    ListView lista_teste = (ListView)findViewById(R.id.lista_teste);
    String[] dados = new String[]{"xxxxx","xxxx"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dados){
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            // Get the Item from ListView
            View view = super.getView(position, convertView, parent);

            // Initialize a TextView for ListView each Item
            TextView tv = (TextView) view.findViewById(android.R.id.text1);

            // Set the text color of TextView (ListView Item)
            tv.setTextColor(Color.WHITE);

            // Generate ListView Item using TextView
            return view;
        }
    };
    lista_teste.setAdapter(adapter);

1 answer

1

John, for this you have to add the event setOnItemClickListener, see an example of the click event and opening a new screen:

lista_teste.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent mIntent = new Intent(context, SegundaTela.class);                
                // chama a nova tela
                startActivity(mIntent);
                // destrói a tela atual
                finish();

            }
        });
  • in this example any item I click will do that is not?

  • Yes. I forgot to put a command. After executing startActivity it is necessary to destroy the current screen with the command Finish();

  • da para separa por posição tbm tipo se item position 1 selected Open Screen 1 If position 2 selected Screen 2? I searched and did not find, in case this would be my need

  • yes, within the onItemClick method, it brings the position parameter, which is the element that was clicked. You can do, if position = 1 opens screen 1, if position = 2 opens screen 2 and so on.

  • vlw in thank you very much

Browser other questions tagged

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