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.
Related
This is not code Question.
As I saw on some website, RecyclerView's setAdapter should be run only once the first time.
What is the reason?
If so, in the Nested RecyclerView, the person of this video keeps calling the
setAdapter() of the Sub RecyclerView every time it is recycled in
onBindViewHolder(), is it wrong??
https://www.youtube.com/watch?v=EyUjw6b5gXE
ADDED
No he is right I think. Let see the logic which was proposed in the video. You have parent list with child items. It is ordinary situation as I know. But every child item in parent list is parent item for its list which is stored inside the child item of the parent list. So when you call setAdapter() inside container of parent list you send data to its children and their children And what about onBindViewHolder(), why setAdapter is called every time? It is very simple, let see into documentation:
RecyclerView calls this method to associate a ViewHolder with data.
The method fetches the appropriate data and uses the data to fill in
the view holder's layout. For example, if the RecyclerView displays a
list of names, the method might find the appropriate name in the list
and fill in the view holder's TextView widget.
So when you send data for the first level RV you associate child RV with some data for which purpose you call setAdapter in each item of parent list. So it will be called in every position of the parent list item to assign data to the child list which also will have some data holders with its handling. Let see life example:
grandfather (activity) bought some candies for his children (first level list items) who will give some of them to their children (second level lists which are items of first level list items)
and for passing some candies between among relatives we use setAdapter() method :) I hope I explained your question and you will understand this video :D
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.
I want to make a list of SMSs in my own application..
my question is about the steps makes by the adapter (automatic)
-when new message arrived I adds it to list object (not the ListView).
-then I passes the list to the adapter of the listView.
the adapter GetView() method run for every Item in the list
-I notify the listview about the change.
- the listview re-draws all its existing rows and then draw the new row.
My question: this behavior (re-draw and redraw, it mean every row will be drown times equal to the total rows) affect the performance?
*if the question is not clear I say: does the ListView Draw all the Raws just to add new row? *
The ListView doesn't redraw every single item in order to add one. It will only be drawn when you scroll to it. And yes you have to notify ListView that the change had happened.
I guess you want to add the item at the top position. So all other item's position and index will change . Adapter will redraw the whole list(only elements which are visible on the screen) .
For performance you can use viewHolder pattern.
see this link
[Making ListView Scrolling Smooth][1]http://developer.android.com/training/improving-layouts/smooth-scrolling.html
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 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.