queries with android listview - android

I am trying to create a listview that includes a "remove" button on every list item. The item will be removed from the list when it is clicked. Can any one guide me to how can I actually detect the position of the list item if I am using a button onclicklistener? and should the onclicklistener be placed in? (should it be in the list adapter, custom extended listview class or in the activity which holds the listview?

Your implementation should be in the ListView's adapter. When you implement the getView() method in your list adapter, add the remove feature for the button. The getView() method will pass you the position in the array of that item. Here's some what of a guide to custom ListViews with an ArrayAdapter. Your button's onClickListener should simply call adatper.remove(item) to remove the item.

Related

Get clicked view from onItemClickListener

I am currently designing a store for a game and am facing a problem with the list of items to buy:
I have a ListView containing RelativeLayouts as items - in each RelativeLayout are two inner RelativeLayouts, with the items in them.
I want to find out if the user clicks on BUY in item1 or item2 - but I found no way to get this in an onItemClickListener.
The problem is, if I use onClickListener instead of onItemClickListener, I have the item (one or two), but not the row - so I always have either the row or the column.
Basically, I need to get the ID of the actually clicked Item from within the onItemClickedListener - is there any way I can do this?
Add a Tag to your Button (button.setTag(position)) in the AdapterĀ“s getView() and then in onClickListener call view.getTag() to retrive an index
You should add onClickListner to your Child RelativeLayouts.Do it from your adapter getView method.
you set onClickListener to your buy Button. To the buy button you can attach tag of the position and retreive it in its onClick method.
Inside getView()
btnBuy.setOnClickListener(this);
btnBuy.setTag(position);
in onClick method
int position=Integer.parseInt(view.getTag().toString());

ListView Highlight item without user intervention

I hava listview which is in sidebar. This listview has 5 items and for click on each item an new activity is launched .
So , this sidebar listview also is part of that activity . I cannot use CLick Listener event to highlight listview since each click launches an completely new activity and hence a new view.
The only option seems to be to highlight the item in listview in ONcreate() Event or some other hack.
How can i create an highlight an item in listview when the listview itself is replaced on each click on listview item
You are going to want to write a custom list adapter. Extend a custom class from base adapter. The override the getView(...) function. Inside the getView you can check for a condition and then highlight the item/changebackground/etc
Assuming the sidebar ListView is the same for each activity, then it should be a simple matter of passing the index of the ListView item that was selected to the new activity. Then in the new activity's onCreate, highlight the correct item in the ListView. I'll leave the implementation up to you but it should be fairly straightforward.

Overriding Android ListView onItemClick causing item to be selected (checked)

I am trying to better understand the internal functioning of ListView as it pertains to selecting one or multiple items- it's actually amazing how difficult it was for me to even get this far in my understanding.
By default a regular click on a ListView item is setting the 'checked' state for that item to true. How do I override this behavior so this selection does not happen?
And more fundamentally, what are the underlying ListView mechanics here? Is the row view's default onClick then calling the ListView's onItemClick/LongClick handlers, or how does this click handling get sequenced?
I do want to allow a choiceMode of multipleChoice, but I only want to select it onItemLongClick. Overriding onItemClick does not change this behavior, and overriding the row view's onClick handler in the adapter getView() function seems to prevent the ListView onItemClick and onItemLongClick from ever happening.
Below is more detailed context on my application
My goal is to have my ListActivity display a ListView, which functions as follows:
Clicking an item performs a non-selecting action (expands the row to show more info)
Long clicking an item selects it. Selecting an item is indicated by highlighting the background of the row (as in the Gmail app)
You can select multiple items
My application structure is:
Activity is an extension of ListActivity
Adapter is an extension of ArrayAdapter<>
ListView row layouts are completely custom layouts (not any sort of built-in ListView row layout)
My understanding of the built-in functionality for ListView has me to the point where I am
setting choiceMode to multipleChoice
using the ListView 'checked' functionality for making and tracking the selections
using a custom selector as an 'activatedBackgroundIndicator' to show the highlighting (example here)
Keep an ArrayList to maintain ListView items selected position. When a ListView item is selected check in that ArrayList whether item position is in ArrayList or not. If item is not in ArrayList change state of Item to checked else change state to unchecked and remove the position object from ArrayList. This worked for me.

I have a ViewPager which contains multiple ListViews, where do I listen for an item being clicked?

I have an activity which contains classes for my list adapter and my view pager adapter as well as your standard activity functions.
The result is the ability to swipe through multiple lists.
I'm not sure where to listen for an item click. In the main activity? In the list adapter? view pager adapter? how?
Thanks
For each ListView you have to set an OnItemClickListener within your ViewPagerAdapter. You can either create a separate listener for each ListView, or give each ListView a tag and use a single listener that checks the tag and responds accordingly.

Android ListView: How to use the activity onClickListener from a custom Adapter?

I have a listView using a custom adapter. Each row contains a button and some other Views. I want to be able to click either on the button, or on the row itself (to edit the item of the list that is clicked).
Setting an onItemClickListener in the activity won't work because of this problem
I think I have to set an onClickListener in the getView() method of my adapter for it to work properly.
I would like to use my activity's onClickListener, in order to use a startActivityForResult() when the row is clicked, in order to have something returned to my activity when the item edition activity is over.
How can I do that?
Thanks!
You'll need to add an onclick listener to every button you add to every row. The best way to do this is probably to make your own custom layout in code, and every time you create a new view in your adapter, set the onclick listener in the layout code.

Categories

Resources