android developer
android developer

Reputation: 1253

click event in the getView method of adapter in android

I have a problem.

The click event of the image view is not getting called in the getView method of adapter. I am perplexed as to why is it happening so. The code to me seems to be ok.

Following is my code:

//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent)
{
    ImageView imageView = null;

    if (convertView == null) {
        imageView = new ImageView(act);
        imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        imageView.setTag(position);

    } else {
        imageView = (ImageView) convertView;
    }

    if((Integer)imageView.getTag() == 0) {
        imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
        imageView.setPadding(10, 5, 10, 5);
        Bitmap bitmap = ThumbnailUtils.extractThumbnail(imgPic.get(position).getBitmap(), 200, 200);
        imageView.setImageBitmap(bitmap);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

    }

    imageView.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            //TODO Auto-generated method stub
            if((Integer)v.getTag() != 0) {

                //opens the image to view.
                                    //Intent

            }
        }
    });
}

Note: I have a grid view. The first item in the grid view is a camera image which will always be there. Clicking on this will open the camera. Once the images are captured, they shall be aligned to this camera image.

So the arraylist has a bitmap image for camera and then the file paths of the captured image.

Upvotes: 0

Views: 2225

Answers (3)

android developer
android developer

Reputation: 1253

It appears to be a very weird problem. But I solved it with an alternative.

So for all those who are facing this weird problem,

one alternative can be by implementing the onItemClickListener.

grid.setOnItemClickListener(new OnItemClickListener()
{

        @Override
        public void onItemClick(AdapterView<?> adapter_view, View view, int position,
                long id)
        {
                 if(position == 0) 
                 {
                    //your code
                 }
        }
});

provided the imageview so created in the getView method be setOnFocusable(false)

Upvotes: 0

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Do this way

public View getView(int position, View convertView, ViewGroup parent)
{
    ImageView imageView = null;

    if ((convertView == null) || (convertView.getTag() == null)) {
        imageView = new ImageView(act);
        imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        imageView.setTag(position);

    } else {
        imageView = (ImageView) convertView.getTag();
    }
     convertView.setTag(imageView);
    imageView.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            //TODO Auto-generated method stub
            if((Integer)v.getTag() != 0) {

                //opens the image to view.
                                    //Intent

            }
        }
    });
}

Upvotes: 1

Sumit Dhaniya
Sumit Dhaniya

Reputation: 596

You explicitly need to make an image view clickable. imageView.setClickable(true);

or if you want to capture row click event then create a subclass of OnItemClickListener and attach it to listview but don't make listview items clickable in that case.

Upvotes: 0

Related Questions