Does not recognize a Listview element

Asked

Viewed 180 times

0

I’m creating an event of click, for when a particular LinearLayout which sits on a ListView. The attributes of this ListView are in an XML (item_list), but is not able to seek the linear to make the event, giving NullPointerException.

    ...
    listView.setAdapter(adapterListView);
    beber = (LinearLayout)findViewById(R.id.item_beber);
    beber.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            beberCerveja();
        }
    });

Error occurs on line 2.

  • I was able to solve the problem, just make the event click on Adapterlistview.

  • 1

    As I understand it, this LinearLayout is inside an item of this ListView, right? If so, you will only be able to seek the item_beber to set the click inside your adapterListView, which is probably a class extending from a ArrayAdapter.

  • second line for me is beber = (LinearLayout) findViewById(R.id.item_beber); and that doesn’t imply having to do the event elsewhere, and it just implies that you can’t locate the ID in its context, you can use namespace to do this without having to change anything :)

  • It worked by putting in Adapterlistview, but this issue of namespace I did not understand, could explain me better?

1 answer

2

Use the setOnItemClickListener method()

listView.setAdapter(adapterListView);
listView.setOnItemClickListener(new View.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        beberCerveja();
    }
});

Browser other questions tagged

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