I have a listview with radio button. when i click any option and scroll that list view the previous select will cleared.How to do this. The actual problem is when scroll the view on every scorll the view is refreshed, that's why every time the view is refreshed. How to do this I can't get any solution, Plz. Help.
Check your adapter, you are probably don't do the job right on bindView. You have to set again on bindView the values.
I will reformulate the sentence and you will probably will understand.
The newView creates only 5-10 views (as many they fit on the screen), and they are reused for other rows. If you have a ListView with 200 lines in it, actually you have only 5-10 views, and you have to make sure you update the views with the valid changes in bindView. You have to store/save the changes to an object for later reuse.
Related
Based on the question How can I update a single row in a ListView? I want to update all the visible rows beside the one that I'm changing in the moment. I can take any row that I want with listView.getChildAt(index) but I don't know how can I compare the the returned view with another view that I already have. Their Ids are based on the view definition in the XML file so All the rows have the same Id.
Any help is welcome, thanks in advance.
That question was for a very specific case. If you want to update all rows, then
Only the rows "visually on the screen now" are actual children views of the ListView. It wouldn't even make sense to want to update the others.
Doing it individually for each child view would be, in fact, a very roundabout way of refreshing the whole ListView.
Just change the Adapter's backing data and then call notifyDataSetChanged() on it. It will refresh all visible items (calling getView() for each one), and as you scroll the new ones will be shown as updated too.
My problem is similar to ListView getChildAt returning null for visible children, but despite searching I cannot find a solution.
I have a ListView with a Scroll. The ListView has 10 items, 7 of which are visible and 3 are hidden by scroll. I also have an external method (out of adapter) that must get all of the children from this ListView (e.g. using getChildAt()).
I need all 10 of the items, but the last 3 are null objects. I've tried code like the following:
getListView().smoothScrollToPosition();
But this doesn't work.
I think that I don't need to post the rest of my code, as the description says everything?
As you have already seen you can't get all the child row views from a ListView simply because a ListView holds only the views for the visible rows(plus some recycled rows but you can't reach those). The correct way to do what you want is to store whatever data in the adapter's data and retrieve it from there.
But the ListView doesn't keep the current values from RadioGroup in
running time.
I've seen that you have some problems with this so I've adapted some old code to build a basic example, code that you can find here.
I don't think so you need to add scroll view for a listView. Scroll automatically works on ListView. Try your application without adding scroll view and I'm sure it'll work as you needed.
The reason those children are null it's because they really do not exist and they will never exist, if only 7 children are on the screen at one time, the system will only create 7 and re-use by passing the convertView back to the adapter getView() method.
If you want to grab information regarding your whole dataset you should search on the dataset itself, instead of the views on the screen. E.g. if it's an ArrayAdapter, loop the array; if it's a CursorAdapter, loop the cursor; etc.
The non-visible children of a listView don't actually exist. When they become visible, one of the redundant views is recycled or a new view is generated. So you can't actually access all the views. Why do you want to? Whatever changes you want to make should be made to the data that populates the views rather than the views themselves.
There are a few point that you need to take care of:
1. List view provides inbuilt scroll functionality, So don't use Scroll view. It will only mess up things.
2. List view doesn't contain ALL the children. When you scroll it, it creates only visible items on run time.
3. If you want to get all the children altogether, Better keep an ArrayList of the child objects that your list has. You can add or remove children to this ArrayList as per requirement.
I know it's somehow possible to make a ListView that loads more data when the user has reached the bottom of the list. However, I'm working with a ScrollView, which I have a LinearLayout in, and these two components works great with the scrolling and so. But I don't know how I'm supposed to do so it gets an infinite scroll.
I suppose I need to add something that reads what is shown of the LinearLayout on the screen, and when it calculates that it is the bottom of the LinearLayout that is being shown (by using the current position and the height of the View), it triggers an event.
But as I said, I don't know how to accomplish this. So, if anyone can give me some help I would be very grateful.
EDIT: I found this post here on StackOverflow How to trigger an event when scrollView reach the bottom with Android?, but I don't know what to do with the answer:
Given the requirements, you'll likely be extending BaseAdapter (as opposed to CursorAdapter which utilizes a different mechanism).
Here's a snippet for that:
public View getView(int position, View convertView, ViewGroup parent) {
if (position == backingLinkedList.size()) {
//get more items and add them to the backingLinkedList in a background thread
notifyDataSetChanged();
}
}
If you can trigger an event when you reach the bottom of a scrollview, You could try creating your own type of view to hold your data. When you reach the bottom of the view load the next data into your view class and add it into the layout of the scroll view.
So basically create a class that holds a section of what your showing. Add the first one into the scrollview. When you hit the bottom create another view holding your next data and use scrollviews_layout.addview(view_holding_the_new_data)
I know this was solved a long time ago, but I wanted to share my solution for future infinite scrollers.
I used the onScroll method within the OnScrollListener to trigger a background thread to grab more data and adapter.notifyDataSetChanged to notify the ListView of more data to load and create an infinite scroll. For some reason, onScrollStateChanged wasn't triggering, but onScroll did what I needed so I didn't need to bother.
For the longest time I didn't understand how to keep the ListView from reloading and placing me back at the top of the list, but then I realized I only need to initialize a new adapter and call setListAdapter once, and every time after that, only call notifyDataSetChanged. Before, I was also calling setListAdapter, thereby placing me back at the top.
I have a TableLayout inside a ScrollView, because of some special needs, I need to do some things when the ScrollView has reached the top. Is there such a listener to listen in on user's scrolling operations? Or, if there is no a ready listener, can I subclass the ScrollView (which will be painful I supposed) to achieve that? What methods do I need to implment? So far, I don't see a solution to this, but I hope it's only because I missed something .....
Thank you!
(In case you're wondering why I want to do this, here is some more background:
I have a flat-file database, I use a TableLayout to show the query results. When the query generates thousands of rows, the time spent on populating the table (using TableLayout.addView() to add rows) becomes unacceptable -- one minute on the emulator! And, there is a special need that when the table shows up, it has to be put at the LAST PAGE. That is, I have to populate the last few rows first, then grow upward, while the display remains unchanged when the table is growing. I tried two approaches so far, none was satisfactory. So I had to downgrade the requirement a little --- I don't populate all 3,000 rows at once, instead, I populate only the last couple of pages, like the last 40 rows or so. This way, the user will see the results displayed immediately. Then, if and when he decides to scroll up, when the program sees it, then I populate a few more pages, etc. Therefore, I need a listener or some way to detect when the table or the scroll view has reached the top. Thank you for reading up to this far!)
You might want to look into extending a BaseAdapter http://developer.android.com/reference/android/widget/ArrayAdapter.html and override getView(int position, View convertView, ViewGroup parent).
In that method you can check the position. So if you start out by populating your list with 10 items and the position is 10 then you know you've reached the bottom of your list and can add more items to your list. You know your at the top of your list if the position is 0. There's also a method in ListView to have your items start at the bottom ( like the messenger app ) http://developer.android.com/reference/android/widget/AbsListView.html#setStackFromBottom(boolean).
So I am having a problem with the different pieces of that make up my ListView. I put them into an ArrayList and use a custom ArrayAdapter to hook up to the ListView, which I have done before so I don't believe there is a problem there. Initially the list seems to have the pieces in the correct order, but then I will scroll down the list and the contents will then load in incorrect order. I then scroll back up and everything is jumbled. Has anyone run into this before?
Thanks
-Jake
Yes your problem is related to the fact that List reuses the views for each row. So say your list can see 5 items, but your ListAdapter has 15 things in it. Android will create 5 + 1 instances of your row view instead of 15. One for each row in the list + 1 for when half of the top and bottom can be seen. When a row is moved out of the visible area the List will recycle that view instance for another row instead of creating a new one. If you don't properly reset all of the user interface components every time you'll get artifacts from other rows showing up. You must make sure that every time you bind your data from the objects in your array list to the view you set every field every time.
For a better description of this see
http://www.youtube.com/watch?v=N6YdwzAvwOA&feature=related