Using BaseAdapter to populate GridView - android

I am having some difficulty in getting my implementation of a BaseAdapter working to populate a GridView.
I've a few questions around the workings of the BaseAdapter, based on the example here: http://developer.android.com/resources/tutorials/views/hello-gridview.html
In my implementation, my BaseAdapter.getCount() method returns 130. Therefore I would have expected the getView() method to be invoked once for each count. However, as far as I can see getView() is only invoked approximately 70 times....why is this?
If getView() is used to construct the View object at each position in the grid, then what is the purpose of getItem() which returns an Object?
Lastly, I wish my grid to be 10 columns wide and 13 rows in height. To achieve this, do I just set the android:numColumns attribute to be 10?
Any help is appreciated in understanding this.
Thanks.

However, as far as I can see getView() is only invoked approximately 70 times....why is this?
It generates the views as they are rendered to the screen
If getView() is used to construct the View object at each position in the grid, then what is the purpose of getItem() which returns an Object?
getItem(int position)
Get the data item associated with the specified position in the data set.
As the description says, it returns the underlying data associated with that position as opposed to the View for the same position.
To achieve this, do I just set the android:numColumns attribute to be 10?
android:numColumns setNumColumns(int) Defines how many columns to show.
The description is quite explicit here.
BTW, this last question is one that you could easily test yourself rather than asking here...

Related

Add different first view to GridView in Android

I want to have in my GridView first item different from the others. The gridview's adapter can be either adapter extending from CursorAdapter or ArrayAdapter. It is depending on from where are images path taken - db, or arraylist.
For now everything works fine, but I want to have first element different from the rest. The first element, no matter what is the adapter, has to be empty element which will be button from where I can add extra elements. The image in first element has to be from resource, while the images for rest of the elements are form uri.
Also, there is emptyView of this gridView set. I've tried adding first element into arraylist at the very beginning, but then empty view is not shown. Also, I don't know how it will work with content from DB. To be honest I haven't got any more idea, and also I cannot find anything in Google.
Do you know any way I can add this first view?
I need it to work on API10 and above.
You have 2 options
in the adapter, you can check if position is 0 than inflate the other view.
More complex but seems like better one for you is to override (Assuming you are using ArrayAdapter) the methods:
getItemViewType(int position)
getViewTypeCount()
You can find nice and friendly example here
--- Edit ---
To show your empty view you need to be sure that:
Your View inflated correctly.
you called mGridview.setEmptyView(view);
There are no items to show in the adapter, that means that in your grid view adapter the function getCount() returns 0. Note that this function should be implement with the correct logic after you implements the 2 methods I've mentioned above.

Mono For Android Tutorial: Grid View how is it iterating through each thumbnail

I am going through this tutorial:
http://docs.xamarin.com/android/tutorials/User_Interface/grid_view
I am an experienced programmer but newer to Android so that is probably the issue.
I understand what is happening but I do not see/understand how the GetView() in the ImageAdapter is being called for each individual resource in thumbs[].
I debugged and stepped through the code but still do not see what/how it is calling the GetView() function to generate a view for each thumb and how it is incrementing the count/position counter that calls GetView().
Any help would be appreciated.
The GridView/ListView/ExpandableListView/Gallery classes in Android provide important optimizations that allow you to display long lists or grids of content without drawing the entire list at the same time.
These AdapterView classes have an underlying adapter that provides the AdapterView with a view for any given cell. So even though a ListView may contain 100 items, if only 6 items are visible in the view at a time, then the AdapterView may only draw ~8 items.
The getView() method in your Adapter will be called by the AdapterView when the user scrolls to the position of that item in the AdapterView. The count should not be being incremented, unless the number of "cells" you intend to be visible in the AdapterView is increasing.
Generally in your implementation of getView(), you will want to recycle the existing view if possible and only update the text/images with the data from your model.

ListViews inside ListViews

I have to implement something like that:
"ListViews"
I call month.setadapter(adapter) once for each year but it's not working because the last month overwrites the information of the previous last one.
Please help me regarding this.
What I normally do in a situation like this is implement it as a single custom adapter.
Derive from BaseAdapter
Set your data as a non-generic ArrayList. You can fill your ArrayList with data representing your month rows and the detail rows within each month. You need some way of differentiating them in the adapter, so wrap things in custom objects that allow you to do this. In your example above, I'd say you can iterate through your data and insert a Date object each time you reach a row with a different month than the previous data row.
Your adapter should override getItemViewType and getItemViewCount. getItemViewCount returns the total number of different view types (looks like you'll return 2 from this method).
In getView, take the position parameter and pass it to getItemViewType. In getItemViewType, get the item in your ArrayList at the current position and test it for its data type. Return a constant representing which type of data to display.
In getView, now that you have the data type to display, run code appropriate for this display - inflate the layout representing the appropriate row type and set the appropriate data on the child views of the layout.
All of this is covered in the "World of ListView" video from Google I/O 2010 that ALL Android programmers should watch at least once:
http://www.youtube.com/watch?v=wDBM6wVEO70

Android: Is it possible to base a GridView on an adapter with filter capabaility?

BACKGROUND:
I have a GridView based on a fairly complex Adapter class which extends BaseAdapter. I find that I am doing much too much work in getView to calculate what to display and that often the number of items I want to display changes during those calculations. getView even gets called multiple times for the same position. It just seems like notifyDataSetChanged() gets called too soon.
This has caused much pain in my coding. I moved most of the code out into other routines, but find I still need to do some filtering of selections during the final display.
I've worked around this by setting the item.enabled(false), which works, but I'd rather eliminate the item all together.
I can't find a lifecycle for the GridView & underlying adapter, but I have determined that getCount() gets called a number of times before the items are displayed and while I can hook my changes in there, it just feels like I am having to work too hard here.
QUESTION:
Can I use a Filter class in connections with an Adapter tied to a GridView?
For example, my Adapter for the GridView has 36 items to display, and the Activity which contains it has a TextView set to value "3" which means only show every 3rd item. adapter.notifyDataSetChanged() gets called and immediately .getCount() wants to return 36, which really messes with getView
I know there are not a lot of specifics here, because I need to understand the concepts, not just find a solution for a specific problem.
There must be some functionality or way of looking at the problem that I am overlooking.
Have you overridden getCount? Why would it return 36 after the underlying data has changed?
If you make getCount depend on the underlying data I don't think you would have this problem:
#Override
public int getCount() {
return myArray.length; // or whatever your underlying data source is
}
Edit - Also, make sure you make changes to your data set before calling notifyDataSetChanged.

How to optimizie ListView with different item's layout

in my listview I have items with different layouts, in fact they use one sub-layout few times. I cannot use getViewTypeCount() and getItemViewType(), because I don't know how many times the sublayout will be used. Is it possible to optimize somehow getView() method and use it's convertView parameter, or do I have to inflate view each time?
Since you know how many types of layout you would have - it's possible to use those methods.
getViewTypeCount() - this methods returns information how many types of rows do you have in your list
getItemViewType(int position) - returns information which layout type you should use based on position
Then you inflate layout only if it's null and determine type using getItemViewType.
Look at this tutorial for further information.
UPDATE:
To achieve some optimizations in structure that you've described in comment I would suggest:
Storing views in object called ViewHolder. It would increase speed because you won't have to call findViewById() every time in getView method. See List14 in API demos.
Create one generic layout that will conform all combinations of properties and hide some elements if current position doesn't have it.
I hope that will help you. If you could provide some XML stub with your data structure and information how exactly you want to map it into row, I would be able to give you more precise advise

Categories

Resources