0

I'm using a ListView to show some images and names in my application. I can make the ListView tiles clickable and start new activities as well . But now i want to do different actions for the different items that are on one listview tile. If user clicks on image, image opens and the textView will show some toast or something. The ImageView and TextView are on the same ListView Tile. How can i achieve this? Any Help?

3 Answers 3

1

Define OnClickListener events in your adapter for the button and textview as required and you are done. They will precede the click event for the list item by default.

if the contained views in your custom row layout does not have a click event it will fall back to the click event of the list view item.

Sign up to request clarification or add additional context in comments.

7 Comments

do i need to use some ID/Position like on itemclicklistner of listview? or simple imageView.setOnClickListener will do the job?
it will do the job. and yes you will need the id to get the ImageView in your custom adapter and then set the onClickListener
Thanks bro.. it worked.. but It sometimes mix the names with other listitem's names. I've three names Jhon, Sam and dee. if i click on jhon it will sometimes show sam is clicked and sometimes if i press same it shows dee is clicked. But after the list is kinda loaded completely it works fine.. is it some coding error or is it just normal behaviour?
That's strange. How big is your list that you get time to click before it loads completely? If it is too long and you are able to click before it completely loads, I'm assuming it is because the indexes are changing by the time you click and the onClickListener reacts to the click event. But, this is just my assumption.
The index thing, may be thats the reason because i'm loading the data from online source and it sometimes take some time to load completely.
|
1

You can implement click event in adapter, but you also implement it in activity. You can find solution in :http://www.migapro.com/click-events-listview-gridview/

hope it can help you. Some note: should not use view holder in gridview like above tuturial, it can make some bug, can implement direct like this:

button1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    ((ListView) parent).performItemClick(v, position, 0); // Let the event be handled in onItemClick()
}

});

Comments

1

Another way to implement this elegantly is to add secondary views called 'accessories' to the item views associated with your ListView. This is explaned quite well in the link below by Cyril Mottier: A Google Developer Expert on the Android platform:

http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.