Maciej Wojcik
Maciej Wojcik

Reputation: 2171

Android - gridView onItemClick

Hi i have strange problem with my gridView. I set on this itemClikListener and it doesn't work. Structure of view is this: GridView -> LinearView with ImageView.

When i set itemClickListener as below it doesn't work:

    GridView board;
    board.setAdapter(boardAdapter);

    board.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Log.d("CLICK", "onClick");
        }
    });

This is't working, my console is clear.

When I implement listener on each of imageView (on each item of gridView) separatly in my GridViewAdapter -> boardAdapter everything works fine.

Fragment of working code in Adapter getView()

imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("CLICK", "onClick");
            }
        });

Why I can't appy the listener to whole gridView and their items? I need have this listeners in class that have fields gridView, and gridViewAdapter, not in gridViewAdapter?

I searched in stackoverflow but i can't make it. Please help

Upvotes: 0

Views: 1918

Answers (3)

Ashish M
Ashish M

Reputation: 823

One problem in you code is missing @Override annotation.

board.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int     position, long id) {
        Log.d("CLICK", "onClick");
    }
});

If still that doesn't works. Then may be any of your Grid Item's child view is also listening for click event. If you know about the even handling procedure of android Touch Framework then you probably understand my point. Else It will be very good to know how android Touch Framework works. You can Learn about that here.

Upvotes: 1

Atef Hares
Atef Hares

Reputation: 4881

Try to add these to your GridView Items's layout.xml

android:focusable="false"
android:focusableInTouchMode="false"

Also make sure that your Adapter returns true for the method

isEnabled(int position)

Upvotes: 1

Sanjeet
Sanjeet

Reputation: 2403

@Override annotation is missing for implemented method. Also anonymous inner class should refer from AdapterView. Please see below sample code.

board.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        Log.d("CLICK", "onClick");
    }
});

Upvotes: 1

Related Questions