How to click on the Listview item and call another screen?

Asked

Viewed 5,215 times

1

I created a listview where it shows my registered items, as I do so when clicking on the desired item it opens a new screen with the description and details of the item?

Class where displays my table:

public class MostraTodosOsLivrosActivity extends Activity {
  private ListView lvMostraTodosOsLivros;

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

    Button btCadastro = (Button) findViewById(R.id.btAbreCadastro);
    lvMostraTodosOsLivros = (ListView) findViewById(R.id.lvMostraTodosOsLivros);

    btCadastro.setOnClickListener(new OnClickListener() {           
        @Override
        public void onClick(View v) {               
            Intent it = new Intent(MostraTodosOsLivrosActivity.this, CadastroLivroActivity.class);
            startActivity(it);              
        }
    });

}

@Override
protected void onResume() {
    super.onResume();

    DbHelper dbHelper = new DbHelper(this);
    List<Livro> listaLivros = dbHelper.selectTodosOsLivros();

    ArrayAdapter<Livro> adp = new ArrayAdapter<Livro>(this, android.R.layout.simple_list_item_1, listaLivros);

    lvMostraTodosOsLivros.setAdapter(adp);

}
  • Do you at least already have the screen that will possess these details? I think it’s a little broad your question as it involves starting the Intent, pass the data to it, create a new Activity, its layout and even set the received data.

  • I have @Paulorodrigues, I was only missing the method that calls the screen. The answer below solved my problem. Still obg by the attention.

1 answer

5


You must set the event setOnItemClickListener of ListView:

lvMostraTodosOsLivros.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // se for a partir de um fragment
        Intent intent = new Intent(getActivity(), /* activity a ser chamada */ DetailsActivity.class);
        // se for a partir de uma activity
        Intent intent = new Intent(getApplicationContext(), /* activity a ser chamada */ DetailsActivity.class);
        // set no intent o id ou a position do item selecionado
        intent.putExtra("ID", id);
        intent.putExtra("POSITION", position);
        startActivity(intent);
    }
});

And in the Activity of details in the method onCreate receives the values of the identifiers from which item was clicked:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    long idSelected = getIntent().getLongExtra("ID", 0);
    long positionSelected = getIntent().getIntExtra("POSITION", 0);

    // Então aqui você utiliza o ID do item(caso tenha) para pesquisar no banco de dados ou a position para pesquisar na list de origem
    // E então setar a View com os detalhes do item.
}
  • Perfect just what I wanted only that I’m having a doubt. Example I have a product "Coca Cola" and another "Potatoes" to descriptions of the coca and different from the right potato... When I click on the coca or potato always appears to msm screen. how do I call each one a different screen? I’m making a screen selling products know? Ai one has to have a description with different price.

  • @Itallofreire, I edited the answer see if it helps you.

  • @Fernado Help yes. Just that I want an opinion, I’m developing an app where you have the Seller and the customer. I need my seller to be able to register the products in comics with price and quantity (radiobutton), so that the customer can make his order. Summarizing so that my customer (seller) can register their products directly in the app, would you recommend a Listview ? Or you know some other way I can do it ?

  • Until now in my project I have a screen of categories with Imagebuttons, a screen with listview with the name of the products and the last screen with the description, price, quantity, photo and the button "Select". What is most recommended for me to do this project?

  • @Itallofreire, There is no way I can recommend you anything, because I do not know your project, nor your requirements nor your needs (you just explained briefly), even because this would already be completely broad, opinionated and outside the scope of the original question. So what I can recommend is that you continue your implementation according to your needs and your client, and as soon as you have doubts about programming or implementation go posting them clearly and objectively here on the site, that the staff helps you.

Browser other questions tagged

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