How to click on Listview

Asked

Viewed 705 times

0

Well I created a search screen with Pain text and added a Listview to show the suggestions, but how to set the click of the item that appears in the list?

Java code

public class Tempo_Real extends AppCompatActivity {
    EditText edtlinha,edtparada;
    Button btmbuscar;
    String linhas [] = {"Alvorada","Maracana","Res.Salvação"};
    private ListView lista;
    ArrayAdapter<String> adapter; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tempo__real);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        lista = (ListView) findViewById(R.id.Lista);
        edtlinha = (EditText) findViewById(R.id.edtLinha);
        edtparada = (EditText) findViewById(R.id.edtParada);
        btmbuscar = (Button) findViewById(R.id.btnBuscar);
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, linhas);
        lista.setAdapter(adapter);
        edtlinha.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                adapter.getFilter().filter(s);

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {
                // Faz o que você quiser. Com o parâmetro "position” você pode obter o item correspondente do Adapter de acordo com o item clicado.
            }
        });

    }
    public void btnBuscar (View view) {
        String stlinha = edtlinha.getText().toString();
        String stparada = edtparada.getText().toString();


        if(stlinha.equals("Alvorada")&& stparada.equals("CA1507")) {
            Intent intencao = new Intent(this,Alvorada_Tempo_Real.class);
            startActivity(intencao);

        }

        if(stlinha.equals("Maracana")&& stparada.equals("CE1507")) {
            Intent intencao = new Intent(this,Maracana_Tempo_Real.class);
            startActivity(intencao);

        }
        if(stlinha.equals("Res.Salvação")&& stparada.equals("CE1507")) {
            Intent intencao = new Intent(this,Res_Salvacao_Tempo_Real.class);
            startActivity(intencao);

        }


        else if(stlinha.equals("")|| stparada.equals("")) {
            Toast.makeText(getBaseContext(),"linha ou Parada não Cadastrada",Toast.LENGTH_SHORT).show();
        }

        else {
            Toast.makeText(getBaseContext(),"linha ou Parada não Cadastrada",Toast.LENGTH_SHORT).show();
        }
    }
}

3 answers

0

I took a look at your code and noticed an existing method that should do this function.

In your code you have this method that serves to capture the Click events:

lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {
                // Faz o que você quiser. Com o parâmetro "position” você pode obter o item correspondente do Adapter de acordo com o item clicado.
            }
        });

this method is not working or you want another alternative to it?

0

ListView listView;
...
listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // Faz o que você quiser. Com o parâmetro "position” você pode obter o item correspondente do Adapter de acordo com o item clicado.
        }
    });
  • Ola Márcio did the most method when opening the application it closes with error "The application stopped"

  • My code is generic. Did you adapt it to the name of the object you were already using? Put your code up to date and what exact error it gave.

  • So put code without change there you show me where I can modify ok?

  • I edited the Question with the updated Code, remembering that I did not make an adaptor class for Listview

  • The code of listview seems correct, try to post the error that appears in the Run window of Android Studio. It tells you which error you made and which line of code. Just looking at the code around here doesn’t help much. I recommend you take a look at these 2 courses. I have done and learned a lot: https://br.udacity.com/course/android-development-for-beginners-ud837/ and https://br.udacity.com/course/android-basics-multi-screen-apps-ud839/

  • Ok the error when running the app in my case I use my mobile phone, by clicking on the button that takes me to the search screen of the following error "bus project stopped working". When I remove the code setOnItemClickListener opens normal

  • Good after restarting the PC I decided to test and the app ran normal but when I click on the item nothing happens

  • Nothing happens because the method I posted only has the shell, rs. You have to implement something within the method to make something happen. Dude, I suggest you actually take a look at the courses I posted above. I tried to start developing Android by trial and error and was just wasting time and nothing came out, because they are many new concepts, even for me who was already programming for PC. These courses are very didactic, because part of the.

  • Ha yes as I said I’m beginner, however, don’t you long because I need to present Monday in my class more worth

Show 4 more comments

-1

mListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            //aqui vc irá fazer a acao com o click pegando uma posicao da sua lista, coloquei um exemplo de iniciar uma nova tela passando o item clicado por parametro.
            Intent intent = new Intent(this, MinhaProximaTela.class);
            intent.putExtra("mParametro", mParametro[position]);
            startActivity(intent);
        }
    });

Browser other questions tagged

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