I want to know that what will be the best way to implement spinner in a listview. My scenario is I am getting products names, prices and their number from server side. For this I am going to make one main Activity (with one xml) that will contain the listview and a separate adapter class to make the adapter for the listview and that adapter layout. Now I want to ask is this approach will be good as I will be having some operations on spinner selection (getting each spinner (or product) dropdown number, spinner (or product) name and position) and also I will have to handle spinner positions as getview() will render each time? Or you people suggest me some good ideas instead of this?
Having many spinners in a ListView is possible but I would imagine in most cases it'd be inconvenient for the user -- too much clutter and moving elements on top of each other. I suggest you create a separate Fragment for item details, and only display static items in your ListView that will take the user to item details fragment (with spinners and anything else you might need). Take a look at this design pattern: Multi-pane layouts
Related
In most case, we need to implement the BaseAdapter’s getItemViewType() and getViewTypeCount() for dynamic item content of the listview, as this post says. But I think this solution is only suitable for finite number and knowing beforehand, such as listview item with sending layout and receving layout.
What about the case that the listview with its item content impossible knowing beforehand?
For example, I need to show a contact list from server, the contact list size is about several thousand. For each item, I need to show, for example, the hobby “list”. It is a small range of 0 to tens of string. So in this case:
The item types is relatively bigger than normal case using “getItemViewType”
Though each item may be different, but similar to a certain degree: the item content is different in the number of views, but common in view type. Item A is different from item B only because it have more TextViews.
For each time in the getView, the convertview is hard to simply reuse because they are different, but if we create new TextView and added to the convertView, it will impact the scrolling of the listview. I don't think it's appropriate such way. What should i do in such case?
Unfortunately you cannot easily change the number of item view types on the fly. The getViewTypeCount() method is only invoked when the adapter is set on the ListView. That means, were you to dynamically change it, then you would have to call setAdapter() again. This is a huge performance hit as the ListView will toss out all the recycled views and re-generate everything from scratch again.
Honestly, it looks like you should be using an ExpandableListView instead. It allows you to displays lists of items under groups. The only difference is the groups are placed on top of the list. So where you have A, B, C, D on the left side in your picture...in the ExpandableListView it'll sit on top.
The ExpandableListView can easily handle your situation where a given grouping could have any variable number of items within it. You mentioned needing to store a contact list. I'd suggest taking a look at a RolodexArrayAdapter for use with the ExpandableListView. It may be of help.
When it comes to a screen that contains 2 different lists depending on the state of say a checkbox at the top, is it better to use 2 listviews that you hide/show when the checkbox is selected or should I have 2 different adapters and attach/detach each from a single listview?
Are there any benefits/downsides to either of these solutions?
You should go with 2 adapters and 1 ListView. the reason is simple - either way you are going to have 2 Adapters (as per your question). Having one ListView would make the code simpler and there will only be a single instance in your xml file. Depending upon the state of the checkbox, you just need to change the adapter the listview points to and notify of this change.
If you had two list views, you would have to hide one. Just because the ListView is hidden doesn't mean that Android does not have to bother about it. You just have an object (size will depend) consuming resources sitting in the background.
Both of the proposals are wrong.
If we assume that you have same Object type to be listed in a ListView (Let's say a user object with name and id fields), then you need to have one ListView and one Adapter. When user wants to switch the data, what you need to do is to send the new data set to your adapter and then refresh it by calling notifyDataSetChanged(); method of adapter.
If you are creating a very dynamic list, say, where every row can have a different set of input types plus optional buttons, and the list length is based on another dynamic value, is it better to do this in a list adapter or creating a custom view in a scroll window?
After struggling with list adapters for quite a while now something finally occurred to me- this seems dumb. It seems like I am going through a lot of work keeping track of what spinner is set to what value, which row was clicked and so forth.
For example, say you are showing something like a contacts screen with various details that can be entered about a contact. Some rows will have text inputs (name, address etc), some will have spinners (ie. state, group), some will have checkboxes (like 'favorite' or something). Also, there is an 'add' button that allows you to add another field to edit. Is it worth making this in a list adapter or is it better to populate a custom view, and if the "add" button is clicked, we re-create the custom view, adding a view of the type they want to add?
I hope this is clear.
ListViews (and List Adapters) are meant for data that is to be displayed in mainly similar views. For your example, it is much easier and more natural to have a predefined layout file with the screen and use view visibility so select which views are to be shown. If you need to add views to the screen you can do this dynamically by using findViewById on the layout and then using it's addView method.
Let me know if you need more clarification or sample code...
I currently have one long ListView in my android application and I would like to separate that one ListView into multiple pages so that when the user selects the next button, that will be in the actionbar, it goes to the next set of ListView items.
How do I go about achieving this?
You can write your own ListAdapter/ArrayAdapter and have it only load the items you want to. The arrayadapter is good and easy enough to use.
ArrayAdapter example,
here is another example
I am programming for android 4.0
I would like to create a listFragment divided in 2 sections. This means i want it to be 1 long scrollable list but with a divider between the online items and the offline items. And of course when one item comes online it should jump upwards + the other way around.
All the items are clickable but the divider shouldn't be (and preferably have a differend colour)
How can i do this or is this even possible?
Ok so , a fragment is basically an activity and you can treat it like an activity, in your case you should extend ListFragment (which will act like a ListActivity in a sense).
now, a List Adapter (which populate a list) in its default way will only allow you to display a list in its most simple form so in order to achieve what you want (a list that deals certain list items differently) you will need to write your own Adapter.
its best if you get the data in the order you want them to be displayed so if you can sort the "online" items from the "offline" items straight from your data source you should query it this way. so now the only thing you need to add is a separator between them and you can do it by finding the first "offline" item and inflating a separator above it (this is done inside your adapter).
each task by its own has dozens of tutorials and Q&As around the web and on StackOverflow.
hope it helps and i'm here if you need more help.