Android ListView display a single array into 2 columns - android

I have a single array like ABCD and I want to display it in two columns using ListView like
A B
C D
onClick event should return the position in the array.
Rather avoid to use 2 separate ListView.
Can someone point me to the right direction?

Instead of ListView you can use GridView with maximum columns 2.
set the ArrayList using Adapter.

You should use RecyclerView because it support Grid Functionality which you want to achieve using GridLayoutManager

Related

ListView with two columns, with separate onItemClick - Android

I want to create ListView with 2 columns, something like this:
So I want on different onItemClick for item1 and item2. I can create this layout with custom adapter, but when i click on first row I get InItemClik for whole row. Can someone tell me how can I do this, or give me some useful links....
IMPORTANT:
This is old question, nowadays this type of layout should be created with RecycleView and LinearLayoutManager for one column only or GridLayoutManager for more columns.
add two LinearLayouts(for example) in your custom list xml file and insted of setting OnItemclik listener to list set onClilkListener to these layouts seperately for each.
It should work for you
Use GridView instead of ListView and set it to 2 columns.

How to display more listview items in the same line?

I would like to display 2 listview items in the same line, like favourite contacts in the default phone app. I do not need a full working solution, but only a guideline what to do.
Illustration:
There is a list of favourite contacts and there are 2 contacts (illustrated only by pictures) in 1 line.
Thanks!
You can write your own adapter to handle this, or you can use a GridView.
To use the custom Adapter strategy:
Create a row layout that has space for two elements.
In xml, for whatever you now consider one 'row', make a horizontal LinearLayout that can hold two of them next to each other. That's your new definition of a row.
In your Adapter, fill each row with two elements from your data set instead of the usual 1.
Take care to handle the case where the data set only has one element left.
Your alternative is to use a GridView,

Irregular Gridview, first row with one colum, rest id the rows with two columns

I'm trying to implement an irregular gridview for my Android app. I've defined the gridview to show 2 columns but I need to show just 1 column at the first row. Is it possible using a DataAdapter?
I don't think you can accomplish this with a gridview. The adapter simple provides the data, but the view decides how to lay it out. If your requirements allow you to make the number of columns constant, then maybe you can use the gridlayout instead. I'm thinking you could try to do you layout like the google currents app.
Gridview and ListView are super useful if you have 1000's of items since it reuses views as it scrolls. If you have lots a items in your grid, then I would probably try to use the GridView or ListView to accomplish your goal. Maybe you have to get your requirement changed. Another option is to use a ListView, but sub-divide each row into 2 columns.

Android dynamically calculate number of columns

I have a dynamically created list of items which should be displayed in either two or one column depending on the space the containing text needs e.g. if the text is long it will only be one item per row, otherwise two. Obviously they should all be the same size (half the screen size).
As far as I know there's no standard Android view with Adapter that does that. With a GridView you can have multiple columns, but not some rows one column and others two. A TableLayout could stretch views, but also here you have to know how many columns you need per row. Plus it doesn't have an Adapter.
So what I want to know a) is there any control that I'm missing that supports something like this or b) what would be the easiest solution for this problem?
EDIT: the items also contain a CheckBox and I need to keep track of the checked state so I can't just put two items in one view using an Adapter.
I'd say that this is a hard problem to solve using standard components, due to the problem of mapping data to items to rows.
If you for example use a Cursor with x rows to feed the adapter with data, then the total item count as seen from the Adapter is also x. However, since you're conditionally mapping two items to the same row, it means that a ListView will see y rows in the Adapter, where y <= x. But you cannot easily tell from the beginning what y will be. Furthermore, if the ListView asks the Adapter for item i where 0 <= i < y, there would be no (easy) way for the Adapter to determine which elements from the Cursor that i would map to.
That being said, a viable solution would be to subclass AdapterView or ListView and implement the layout of the elements yourself. As you're getting each item from the Adapter, you'll measure and layout it, depending on the sizes of the surrounding adapter items.
A different solution that could work for you if you don't have a large number of elements is to use two custom Adapters, one called ItemAdapter and one called RowAdapter. The ItemAdapter will inflate the actual items based on (the presumed) Cursor. The RowAdapter will use the ItemAdapter to get the items and merge them into rows. The ListView will in turn use the RowAdapter. The issue is that to know how many rows the RowAdapter will produce, it is necessary to measure all the items from ItemAdapter before the RowAdapter is connected to the ListView.
As far as I know there is no ready solution for your problem. I haven't yet tried something like this, but I would use a LinearLayout as the list item. Then just create your own Deflater (e.g. CursorAdapter) that deflates the Layout and checks the length. If needed you should be able to add a new View (e.g. TextView) to the LinearLayout.

How to page a gridviews in android?

I would like to create pages of gridview in android.
The page 1 will have a 3x3 set of elements.
On clicking the next button it should jump to page-2 with the next 3x3 set of elements.
Right now i have a grid of 3x3 elements and when i give a set of 12 elements, a scrollbar will appear in the right side of the gridview and the next three elements will be shown in the same page by scrolling down.
So please help on how i can implement a page of gridviews.
Thanks,
Sen
Limit the number of items you pass to the gridview adapter.
Assuming that you have an array of items, in your newInstance() method, select from that array 9 elements according to the page. Then pass that array to the gridview adapter. Also, let your getCount method in your gridview adapter return 9.
Sorry I don't give code right now, but if it isn't clear, I will provide done snippets when I have pc access.

Categories

Resources