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.
Related
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
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.
I am using a ListView in combination with a HashList to show data from an SQL Database through JSON. Everything works fine. But now I want to have a special feature.
In the ListView are round about 400 Items. So if i click one of the item to view its content and switch via Backbutton back to the Listview, i want to have the viewed Item from the Listview being marked in another color so that the user sees directly which oh the items he already has clicked.
I painted an little grafik for you, for better understanding
On the left side, the original View. After i View the 1st,5th and 8th Item of the list, the list should look like at the right side.
Is this possible?
You can implement your own adapter, then have an boolean array to store the current status of every row, and in the getView function assign a different color to the background of the selected rows based on your boolean array.
For this you will need also to set an onClick event in your rows to open the previsualization view and there also check the boolean array as "visited".
For more info about how to make custom efficient adapters, please read this
To simply let the user choice more than one row you can use
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
in your list
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 have created one list view.. it is having 5 items...
Now I want split the list items...
when user clickon the first listitem or focus on first item then immediately it has to show followed some text views or other things..but it has to show same list..
and agian same when he clickon or focus on the second item that first item has to be close and second item has to act some thing....
I think you need to implement the concept of "Expandable Listview", so that the clicking on one item, it will be expanded with their sub-items.
Refer the android-sdk page of Expandable ListView: http://developer.android.com/reference/android/widget/ExpandableListView.html
For having an example, check this site: http://mylifewithandroid.blogspot.com/2008/05/expandable-lists.html
Pls, check the below image, do you want to perform as same ????
If you want to do the same, it is already given in the "API-Demos" at Views/Expandable Lists/1. Custom Adapter.
Enjoy !!
The problem is that you cannot use the standard ListView in your case, because in the standard ListView, the View of each row has to be one TextView.
In your case, you need it to be at least two TextViews (The standard Text, and the one that's gonna show up onClick/onFocus).
You have to create your custom ListAdapter, and override the getView() function.
Here is a code snippet that shows how to do it properly:
Custom Adapter
In the getView(), you have to inflate the XML file that describes your List Row, and return it.
In your case, I believe your XML file should contain 2 TextViews, one visible and one invisible.
Then, to handle the clicks, you can set a onItemClickListener on your ListView in your Activity class.
The best way may be to have your Activity class implementing onItemClickListener, and to use this onItemClickListener to handle those.
In the onClick() function, you just have to set the Visibility of your hidden TextView to VISIBLE.
You need to build custom rows, and handle showing more text on each row, there is no easy magicall way of doing it, but inflating your own rows, and setting a couple of attributes visibility isnt all that hard either.