Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting wrong position of the clicked item? #14

Closed
rampatra opened this issue Feb 6, 2015 · 6 comments
Closed

Getting wrong position of the clicked item? #14

rampatra opened this issue Feb 6, 2015 · 6 comments

Comments

@rampatra
Copy link

rampatra commented Feb 6, 2015

I have attached a click listener but getting incorrect position when a particular grid item is clicked. My code is as follows:

mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Media media = mMedias.get(position);

                Intent i = new Intent(getActivity(), MediaDetailActivity.class);
                i.putExtra(MediaDetailFragment.EXTRA_MEDIA, media);
                startActivity(i);
            }
        });

Can you tell me where I am going wrong?

@liaohuqiu
Copy link
Owner

Try id.

@rampatra
Copy link
Author

rampatra commented Feb 6, 2015

Yes, id works 👍 Thanks for the quick reply.

But can you tell me why this happens so?

@liaohuqiu
Copy link
Owner

Like ListView, every sub view in GirdView is a Adapter View. They are managed by a inner view adapter. When they are clicked, the click event will be processed by OnItemClickListener. The position indicates the position of the position of the view in the view adapter. If there is no header views. The position equals the position of the data which is related to the view in the data list.

/**
 * Interface definition for a callback to be invoked when an item in this
 * AdapterView has been clicked.
 */
public interface OnItemClickListener {

    /**
     * Callback method to be invoked when an item in this AdapterView has
     * been clicked.
     * <p>
     * Implementers can call getItemAtPosition(position) if they need
     * to access the data associated with the selected item.
     *
     * @param parent The AdapterView where the click happened.
     * @param view The view within the AdapterView that was clicked (this
     *            will be a view provided by the adapter)
     * @param position The position of the view in the adapter.
     * @param id The row id of the item that was clicked.
     */
    void onItemClick(AdapterView<?> parent, View view, int position, long id);
}

@rampatra rampatra closed this as completed Feb 6, 2015
@PankovSerge
Copy link

Hello, liaohuqiu! I try to use id, but every time i got a 0. Position of selected item changed from device to device with change of columns count.

@OverRide
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Log.d("DEBUG", "Selected item position " + position: + ", with id: " + id);
}

Selected item with position 5, with id 0
Selected item with position 6, with id 0
Selected item with position 7, with id 0

@liaohuqiu
Copy link
Owner

@PankovSerge sorry for replying so late. Is the position right?

@PankovSerge
Copy link

Got this issue when use header view. I use a custom adapter extended from BaseAdapter, and got it working by use id instead of position.

 @Override
    public long getItemId(int position) {
        return position;
    }

One more problem with onClick Handler, if you don't setup it like that

   footerView.setOnClickListener(this);
    }

you have got error in

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

because footerView have position > yourSource.size(). Need to add this moments to README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants