ColinRobertson
ColinRobertson

Reputation: 370

Clickable ListView with Custom Adapter

I'm new to Java and Android development so bear with on this...

I'm trying to create a clickable ListView with a custom adapter. I've got my ListView setup showing each of the rows, but when I try to call setOnClickListener it's complaining that:

The method setOnClickListener(View.OnClickListener) in the type AdapterView is not applicable for the arguments (new AdapterView.OnItemClickListener(){})

So here is my code:

AccountArrayAdapter myAdapter = new AccountArrayAdapter(this, accountArray);        

listView.setAdapter(myAdapter);

listView.setOnClickListener(new android.widget.AdapterView.OnItemClickListener(){
    public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Toast.makeText(getApplicationContext(), "Click ListItemNumber " + position, Toast.LENGTH_LONG).show();

        }
    });

Pretty straight forward stuff. The code is mostly copied from other places, which I think is why I'm having a hard time getting my head around what could be wrong.

Thanks

Upvotes: 0

Views: 1884

Answers (1)

Samir Mangroliya
Samir Mangroliya

Reputation: 40416

use setOnItemClickListener instead of setOnClickListener

listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
            Toast.makeText(getApplicationContext(), "Click ListItemNumber " + position,Toast.LENGTH_LONG).show();
            }
        });

-See this CustomAdapter ListView

Upvotes: 4

Related Questions