Android- ListView items with dynamic content - android

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?

Related

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

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.

get text from specific position in listview android

I have a ListView with custom_row , every row has a textView1 and a textView2 , the list has now 2 records , and i have a button that is not on the list.
When i click the button i want to get the text from textView2 of the 2 records.
Is it possible?
I would take a shortcut, you ListView is being populated by an Adapter that uses a dataset. This dataset can be almost any datastructure such as Array, ArrayList, etc.
The layout you define, such as custom_row in you case only defines the structure of your view i.e. "where" items will show within on item on the list.
On the other hand, it is still your responsibility to tell the ListView "what" to show within the textView1 and textView2. You do this using the Adapter which connects the ListView to the dataset. More often than not, the ListView is a one-to-one mapping of the dataset i.e. the first item on the list is the first item in you dataset (I don't know what you are using for only two items, might be an array).
The ListView calls getCount() on the Adapter to find out how many total views there will be. It then call getView() for each view to be shown on the screen. It is in this method that you define what will actually show in a single view on the list (your custom_row).
Now you would know which entry of the dataset is supposed to populate which view in the ListView so you can just read it off there. For example, if your getView() does:
textView2.setText(getItem(position).getSomeTextField());
And the original dataset is an ArrayList named listDataSet
You could just do listDataSet.get(2).getSomeTextField()
NOTE: You will have to manage the scope of the dataset so that it's visible from wherever you are calling.
Get back your ListView (maybe already stored in an object thanks to findViewById, or by calling getListView() on your ListActivity).
Then call getItemAtPosition() on your list view, with the position you want.

How to Create ONE list which can have a string or image as an item in the list

I want to create a journal that appears as a list in a ListView. The list should be able to have a text entry or an image for each row. Now, I have these entries (text and image) in a database. I just want to know how to create a list adapter which can have these entries. From the examples I have seen, the list adapters all have only one data type. I want my adapter to have data type image or text. Please Help! Been searching for 3 hours now.
To show images in some listItems and text in others, declare both an ImageView and a TextView in the listitem xml, and hide/show one of them with View.setVisibility(...), where you bind the content. For examples of multiple children in a listItem, refer to other Nik's answer

Android CustomHow to get Child Views from Custom ListView enclosed within linearlayout without clicking listview

I have a custom listview with two buttons and two textViews enclosed within linearlayout. I want to iterate through the listView without clicking the listView rows. When i do
listView.getChildAt(position);
position can be any integer from 0 onwards it gives null output, what is the other way to get views from listView. Please help.
There isn't really an interface exposed for getting the view objects from a ListView. These are constantly being switched out and stored for recycling as rows scroll in and out of the visible area.
However, you can do:
listView.getItemAtPosition(position);
to get the model (item) at a given position in the list. If you need to operate on information that the user changes in your list, then you would want to first save that information back to the underlying model individually when each component changes and then later aggregate over your underlying data objects to do your calculation.

Categories

Resources