ListView Highlight item without user intervention - android

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.

Related

Android listview row detail page sliding

I have ListView which has many items(rows), onClick of any item goes to the DetailActivity which explains more about that particular item. If I want to see the detail of the next item, I have to come back to the ListView and click next item. How to design to see the next or previous item's detail from DetailActivity by swiping it left or right.
I assume that you have a Custom Object and ArrayList of this object.
You need to have an adapter to Show this Arraylist into Listview
When you clicked the list item you need to pass your object from ListActivity to your DetailActivity
I think you are doing this fine until this part.
Then now, you can use ViewPager and its adapter in your DetailActivity.
When you click the list item, you need to pass your Arraylist and your object index to the DetailActivity.
In the DetailActivity, get your Arraylist, set your List into ViewPager adapter, find your object via index(that you clicked) and set ViewPager page index as your wanted item index.
If you manage this correctly, you can slide details of your content. You can ask ma anything to make this clear.
There is a tutorial of Using the ViewPager: http://architects.dzone.com/articles/android-tutorial-using
You could go with a ExpandableListView. No need to open a new screen or doing something which becomes hard to manage like Fragments. The user can show/hide detail just by clicking on the item.
A view that shows items in a vertically scrolling two-level list. This
differs from the ListView by allowing two levels: groups which can
individually be expanded to show its children. The items come from the
ExpandableListAdapter associated with this view.

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.

Android ListView set two different adapter. ListView is not updated to newly set adapter until I scroll it

I have 2 different adapter for a ListView. Both adapters extends BaseAdapter.
I have 2 Button in the header view of the ListView.
Button 1 call ListView.setAdapter(adapter1);
Button 2 call ListView.setAdapter(adapter2);
The first time I click either one of the button. The ListView update to another adapter view immediately.
However, the second time I click either one of the button. The Button listener will not trigger until I scroll the ListView.
I have tried to put a toast inside the button listener. The toast is only called after I scroll the ListView.
Anyone have any idea what is the problem?
The UI in Android is efficient and will only update when it NEEDS to. In the case of your list the UI only updates when you scroll it because it needs to fetch more items for the list. To force an update you need to call invalidate() on your ListView.

Using Spinner in a List Item does not trigger the OnItemClik

I have a ListView in which each List item has a Spinner. I have successfully added an OnItemSelectedListener to each spinner by implementing OnItemSelectedListener in the Activity and adding it in the GetView() of the adapter.
The problem is, I also have to implement OnItemClickListener for the ListView in the Activity. I have done that, but the event is not getting fired for the Item Click on normal list items. But, it is getting fired for the list section headers(which do not have the spinners).
How can I trigger the event for the List items as well?
Try this,
add the below property to your Spinner element,
android:focusable=false
The problem is because spinner is a element with clickable property, which will take the control of your click events and hence your listview wont get the chance to handle the click events. By adding the above property you can make both the spinner and your listview to get worked.
If it still doesn't work then try adding this to the top most layout of your listview element xml ,
android:descendantFocussability=blocksDescendants

queries with android listview

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.

Categories

Resources