I only set the num columns in properties of a GridView. Nowhere to set the number of items
I google but only got some questions about how to fix the rows of GridView. I looking for in API GridView also, but there is no function to do that.
Someone teach me? Thanks
You cannot set the number of items directly on a grid view. This has to be done using its adapter and set the number of items in the adapter.
Usually a variable/array holding the items is created before an adapter is passed to the grid. This will populate the grid accordingly.
To be specific, this is the adapter function
#Override
public int getCount() {
return "HOW MANY ITEMS YOU WANT TO SHOW";
}
Related
I want to make an adapter for GridView with 2 type of View accoding to the item position.
The first item for the Grid must be in a View of 1 column and the others items in a View of 2 column. I dont't know if i must custom the GridView or just make it in adapter.
This is an example of the result that i want
PLEASE I NEED YOUR HELP
This isn't likely the answer you want to hear, but the easiest way to do this is to use a ListView instead of a GridView. You have 2 distinct row types, 1 element and 2 element. Use standard ListView conventions to have two different row types present.
To accomplish this, create a new Adapter that extends one of the list adapter classes, override getView(int, View, Viewgroup) so that it returns the correct view based on position (the first argument), and then have it return the correct view for each item in your list. You'll have to manage the relationship between entries in the list and whether they're wide or normal, but that shouldn't be too big of a deal.
I am writing code for a listview which has fixed number of rows and when the user scrolls the listview, instead of moving the list elements (views), I want to change the content of the views where as the list elements (views) stay at the there position.
For this I am returning a number Eg. 5 in getCount(), however I am no clue on how can I change contents of list items while keeping the list items in a fixed position.
Any help is welcomed.
Regards
with getItemAtPosition()
you get the item at position, there you can change its content and then call notifyDataSetChanged() to signal the adapter that the data has changed.
I am having one problem with ListView.
I am generating a list view with dynamic TextViews(white boxes in image) in each item.
The ListView is populated using ArrayAdapter of 'Model' class. Some items can have 2 white boxes, some can have more which depends upon the data/count from 'Model'. Also, I am using ViewHolder pattern and can maintain other states of the list item. But these boxes are added dynamically in getView() method.
So my question is, how to maintain state of these dynamically added TextViews using ViewHolder or any other method when ListView is scrolled.
I guess the no.of boxes changes on scroll, but you need the dynamic no.of boxes to be alloted in a list items?
Solution:
Set the size (i.e) no.of boxes in a list item to an integer array. (like: size[position] = 5).
The size should be set, before the list adapter or base adapter loads the list items.
Now at the end of getView() of base adapter, right the code given below
for(int i = 0; i< size[position]; i++){
//generate textview i.e white boxes here
}
I had the same problem in dynamic list, this solved the issue.
If still you have problem kindly post your the code here.
You need to have a list or some data structure in which to store the content of the data you put in from the ui, for instance a SparseArray<ModelUI>. Your ModelUI class should have some string properties to store the text from your dynamic textboxes.
Also, you need to take into account that you're handling two indexes here: one index/key in the SparseArray which is the real index in your list of Model objects and an index of the items visible in your Listview.
Let's say you have a list of 100 items, in your listview you'll always display 10; you'll always have to refer to the firstVisibleIndex from your listView.
If I understand correctly, you'd like to edit a textbox, scroll 50 items, scroll back to the edited item and see the correct text in the textbox, right?
I tried the following but had no luck since the getChildAt always returns null(I am sure the listView is filled).
listView.performItemClick(listView.getChildAt(0), 0, listView.getChildAt(0).getId());
Any suggestions? Thanks!
Edited: code for adding item into list
lvAdapter = new ActionStreamAdapter<HasDescription>(getBaseContext());
while ((item = actionStreamQueue.poll()) != null) {
...
lvAdapter.addItem(item);
}
ListView.setAdapter(lvAdapter);
Where ActionStreamAdapter is a class extends BaseAdapter. The queue is filled by calling the Dao of a simple data sturcure class.
There is a difference between childs and items. A Child is a view that is lower in the view hierarchy. For example, when there is a LinearLayout containing two TextViews, then the TextViews are children of the LinearLayout. A ListView has items. These are on the same hierarchical level as your ListView so they aren't children. They are items of the ListView. You should use ListView.getItemAtPosition() instead.
Then, there is a difference between selecting and clicking. Clicking means performing an action on an item and selecting is highlighting it. For selecting the first item, you should do something like: ListView.getItemAtPosition(n).requestFocus()
It is an interesting API where it takes a pointer of the viewitem, typically listview items are recycled so the item at position 0, need not be 0th item on the adapter. You could use findViewWithTagTraversal() to find the view and do the selection on it.
If you don't mind what do you want to do with selection, or do you want to give focus to the first item?
You could use setSelection()
I am getting XML data from url and displaying using a custom list adapter in a ListView.
I need to display only 10 items in ListView.
How i can do this?
Please let me know
Thanks
Only put 10 items in the adapter. Or override getCount() and return 10.
The adapter you're using with the ListView only displays the item you put in it.
Just put in your adapter 10 items, or whatever other number you want, and it will display only that items.