How to create a title row in a listview in android? - android

I have a listview in android with several columns, and for each column I want to set a separate title, as shown in this example below (I mean the fields 'Name and Number', 'SMS, 'Phone'):
(The image is just to explain what I want; in my layout there are more columns which are not as self-explaining as in the example).
Is there a good way to do that? Als, is it possible that this title row can be visible at all time, even when I have many many rows and scroll down?

If you want to have a fixed header item in the list that doesn't scroll with the other items, use ListView's addHeaderView to indicate which view(s) should stick to the top.
If you just want the first item to be different than the others, you'll have to code your ListAdapter to have two types of views, one for the header, and another for the rest. This involves
Making getViewTypeCount return 2 to say it can make two different kinds of views.
Making getItemViewType return 0 or 1 to say which type of view will be generated for the given position in the list.
Making getView return the correct view object for the given position in the list.
Presumably you'd want position 0 to be the header view and all other positions to be data.

Related

How to get the view in a custom list view at a specific position? Android?

Let me explain this--
For example - I have a custom list view set up that shows 3 text fields in a group.. I.e. each click able item in that list view will have 3 texts.. so now I want to get the 2nd text view at the 5th index of that listview...How to achieve this?
If a have a basic listview with only one item at each index, then this can be achieved by using getItemAtPosition().
Is this similar in the above case also?
You don't. THe entire point of a listview is that it DOESNT create views for every element in the list- only the ones on screen. So if your list is more than trivially long, it probably doesn't have a view for that item. If you think you need this, you're probably architecting your code wrong for a listview.

Inner List in Android

I have a problem with lists.
I want to implement a scrollable big list, each element of which contains a text on the left side and a NON-scrollable inner list on the right side. (for example: the name of the flat and the list of its inhabitants names)
Both lists should be filled from the cursors and contain an informatio about them: I want to have an oppurtunitz to get all cursos fields when I click an element of the inner list.
As far as I understood I's not possible to solve it with two ListViews. ExpandableListView does not pass me because I do not want to expand the first list, I want to have the inner list always visible.
Do you have any ideas how can I realize it?
Try using a ListView for your big list, and an empty LinearLayout in each item. You fill the LinearLayout of each item programmatically in the big list adapters getView(..) method, where int position is the item number.
See also: android nested listview
Edit: Or use an ExpandableListView, which items you expand programmatically without animation with expandGroup(int position, boolean animate). Prevent collapsing by setting an ExpandableListView.OnGroupCollapseListener, in which you expand the group again. (seems a bit hacky)
Dokumentation: https://developer.android.com/reference/android/widget/ExpandableListView.html

An adapter for GridView with 2 type of view (android)

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.

Items in multiple columns - spinner

I'm creating an Android app and I want to put a lot of short items to Spinner. Basically, it works:
But it doesn't work as I want. As you can see, there is a lot of items which have a very short text. It means that user may be forced to scroll a lot to find an item (s)he want. What I want to do is to put items in multiple columns, but I don't know how to do it. I searched in the internet for a while and the only solutions I've found were "how to put multiple columns in 1 item" - what I want is "how to put items in multiple columns".
Thanks.
First you should create your custom spinner adapter. Then, you can put 3 textviews in a row in your list.
On the getView function of your adapter:
1)Instantiate your custom layout (which has 3 textviews)
2)Give each textview a tag like this:
String mTag1=String.valueOf((position*3)-2);
String mTag2=String.valueOf((position*3)-1);
String mTag3=String.valueOf((position*3));
yourtextview1.setTag(mTag1);
...
// in position 1 you'll get 1-2-3, for position 2 it is 4-5-6 etc.
Thus you can assign the same onClickListener to them. (You should do this in getView function too)
Finally, on the onClick listener, get the clicked item's tag, convert it to integer (which is your items position on the list), get the item on the list with this position, and set your spinner's text with it.

Increase Row Count of ListView with CursorAdapter?

I'm using a ListView that is populated by a custom CursorAdapter that returns two different Views based on the value of the data in the Cursor for that particular row.
One View type contains a TextView, call it TextView1.
The second View contains TextView1 and TextView2.
The problem is I'd like the second View to be treated as two rows in the ListView. So Text1 would become a row and Text2 would become another row.
Obviously with Layouts I can make it look like it's a different row, but it's not actually a different row. I can't independently select them, so when I hook into the ListView's OnListItemClick event, I get a single event for clicking TextView1 and TextView2, they are not distinct. Is there a way I can tell the ListView that there are two rows here and not one or is there a way to fake it?
Is there a way I can tell the ListView that there are two rows here
and not one or is there a way to fake it?
You can approach this in two ways. First you could make your own custom Adapter that breaks the Cursor in the correct number of rows and then simple use one TextView per row with OnItemClickListener. Depending on the size of the Cursor's data, this could be easy or something to avoid.
The second approach would be to use your current custom CursorAdapter and implement two types of rows(implement the getViewTypeCount() and getItemViewType() methods), one with a simple TextView and a row with two TextViews. The trick would be that you can't use the OnItemClickListener on the ListView, instead you would set your on OnCLickListeners on the TextViews from the rows. If you make the TextViews to occupy the entire row's width and height the effect would be of a row being selected. In the OnClickListener for those TextViews you could pass the current position of the Cursor or the id for that row to do whatever you want.

Categories

Resources