简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM