How do you call the ListView items - objects? or elements? or items? what is the right name for it?
You can call it whatever you want. I personally call it items
When referring to the model, i.e. arrays, I say objects.
From ListView
ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.
So, I guess there's no mistake to talk about list items as simply items that are converted to views when the Adapter comes in.
Related
I have a listview with 5 items. Each item has a listview with 5 items. I would like to have this in a single activity using adapter. Is it possible?
Expandable lists let you create lists within lists.
They’re ideal for list items that have sub-categories. The user selects an item from a scrollable list and another list pops open. They can then make another selection from this list. Here is the example and tutorial about this:
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
You can use the Expandable lists.
it will help whatever you want like in your main listview have 5 item and each item have number of more item it will child of that item.
I have a long ListView, which its items are editable (simple strings).
The user can change the content as he/she wants.
My problem is, that when I want to retrieve the new data from the List view,
I can not reach the invisible items in the list.
How can I retrieve the data from these items without scrolling to these items?
Is there a way to fill the adapter ,with the new data the user entered to the list?
I think you can do that. if you have reference of Adapter for that list view
you can call getCount and then iterate and while calling getItem(pos) , to get data for all the positions in the list view.
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 trying to create a list view, in which items can be added dynamically, the dynamic part is working fine, as it can be done using simple cursor adapter and inflating a layout with the list item, each time an item is created. But now the problem I am having is that, I want to swap these items as well, swapping as in replacing item positions. All the examples I have seen use a string array, that is a predefined list. How can I achieve this?
Use an Arrayadapter as your Listadapter. You can now use insert(object, int) to add an item to a specific position in the lists dataset. With remove items can be removed from the lists dataset. Since the Arrayadapter will monitor changes to the dataset itself the list should update once you are done modifying the Adapter.
If you need to use a CursorAdapter this may get harder. You would need to change the underlying database and then requery the Cursor that is used in your list.
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.