Click ImageView within a ListView ListItem and get the position?
Asked Answered
A

4

6

Currently, when the ListItem is clicked I grab its position and pass it to my StopsScheduleActiviy. I would like to detect when the ImageView of that ListItem is clicked so that I can launch my MapActivity instead and pass it the position.

Basically: Click the ImageView launch MapsActivity, otherwise click anywhere else on ListItem launch StopsScheduleActivity. In Both cases I need the position. How can I do this?

Here is my current onItemClick listener.

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

    Intent myIntent = new Intent(this, StopsScheduleActivity.class);
    myIntent.putExtra("stop", stops.get(position));

    startActivity(myIntent);

    Log.i("Winnipeg Bus", "Stop item #" + id + " clicked.");
}

Here is my ListView with a map for an ImageView that I want to listen to for clicks. Thanks!

enter image description here

Adorl answered 20/12, 2011 at 5:25 Comment(8)
position of onItemClick doesn't give the position?Fishbowl
Make custom adapter in this on GetView() method write ImageView's onClick().. And do what you want...Iy
@LalitPoptani It does, but how do I know the ImageView was clicked?Adorl
@Iy Can you elaborate a bit, perhaps with the some code in an answer. Thanks.Adorl
Look at this tutorial Android Series: Custom ListView items and adapters in this adapter's getView method you can write imageView.setOnClick() and your stuff..Iy
Try this and let me know what happen.. :-)Iy
@Iy Thanks for you help. I checked the link, but I don't see where I should set the imageView's onClickListener. I just tried to do it in the getView(...) method of my custom array adapter but get the following error when trying to launch the MapActivity: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? ...Actually, in hindsight the click did register...Adorl
Actually, setting myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); worked. Thanks! Post an answer below detailing what you told me and I'll accept. Thanks again.Adorl
I
12

For ImageView's Click You have to make a custom adapter for your listview.

And in adpater's getView() method just write a imageView's onClick(), And start your new activity from that imageview's onClick().

For custom list just look at this tutorial Android Series: Custom ListView items and adapters ..

Iy answered 20/12, 2011 at 6:17 Comment(5)
Thanks again. For those interested see the main questions comments for more info. Also, Arslan's answer shows code implementation very similar to what I ended up using.Adorl
How to pass a result back from the new activity? How to use onActivityResult() here?Bloodroot
@Bloodroot - use setResult() method set Intent in it. and get this intent in onActivityResult(). Look at #920806Iy
I can write onActivityResult() in Activity only right? If i am firing the intent from my adapter class, where should i write the onActivityResult() ?Bloodroot
In the Activity which calls your Adapter and you are using context to startActivityForResult()..Iy
B
26

In you Custom Adapter getView() method , when you are init the ImageView. Add position as a tag for image view. So each time when new ImageView will appear it will hold its position in his tag. Then just add the OnClickListener() in getView().

OnClickListener(View view) contains the view which was clicked by user. So when user will click any image view in the list. Then it will be passed to OnClickListener(View view) as a clicked view. And we know that our ImageView contains the position as a tag. So the tag can tell us that this ImageView is for ? position. :)

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        
    if(convertView == null){
        convertView = inflater.inflate(R.layout.list_row, parent, false);
    }

    ImageView imageView = (ImageView) convertView.findViewById(R.id.videoListImage);
    imageView.setTag(new Integer(position));
    imageView.setOnClickListener(new OnClickListener() {
            
        @Override
        public void onClick(View view) {
            Toast.makeText(mContext, "ImageView clicked for the row = "+view.getTag().toString(), Toast.LENGTH_SHORT).show();
        }
    });

    return convertView;
}

You will get the clicked ImageView position.

Braided answered 20/12, 2011 at 6:13 Comment(0)
I
12

For ImageView's Click You have to make a custom adapter for your listview.

And in adpater's getView() method just write a imageView's onClick(), And start your new activity from that imageview's onClick().

For custom list just look at this tutorial Android Series: Custom ListView items and adapters ..

Iy answered 20/12, 2011 at 6:17 Comment(5)
Thanks again. For those interested see the main questions comments for more info. Also, Arslan's answer shows code implementation very similar to what I ended up using.Adorl
How to pass a result back from the new activity? How to use onActivityResult() here?Bloodroot
@Bloodroot - use setResult() method set Intent in it. and get this intent in onActivityResult(). Look at #920806Iy
I can write onActivityResult() in Activity only right? If i am firing the intent from my adapter class, where should i write the onActivityResult() ?Bloodroot
In the Activity which calls your Adapter and you are using context to startActivityForResult()..Iy
E
4
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    System.out.println("getView " + position + " " + convertView);
    ViewHolder holder = null;

    if (convertView == null) {
        convertView = LayoutInflater.from(conText).inflate(
            R.layout.gridgoggle, parent, false);
        holder = new ViewHolder();
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.imageView1.setImageResource(Integer.parseInt((mData
        .get(position).get("image"))));

    holder.imageView1.setId(position);
    holder.imageView1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            int id = v.getId();
            //here id is position.
            Intent myIntent = new Intent(this, StopsScheduleActivity.class);
            myIntent.putExtra("stop", id);
            startActivity(myIntent);
        }
    });
    return convertView;
}
Enedina answered 20/12, 2011 at 7:1 Comment(0)
S
0

In three simple steps:

1 In your adapter method getView(int position, View convertView, ViewGroup parent):

ImageView imageview =(ImageView) convertView.findViewById(R.id.imageView);
imageview.setTag(new Integer(position));

2 In layout.xml add the method onClick() on the ImageView

3 implements onClick() method and retrieves the index by view.getTag()

Spangler answered 22/9, 2016 at 15:42 Comment(1)
Please know the usage of the inline formatting `like this`. I'll edit your questionHearsay

© 2022 - 2024 — McMap. All rights reserved.