I have the problem of the ListView re-rendering and changing the values within my list view views. Each cell has interactive elements with numerical values that are set, unfortunately once off screen these numbers are re-rendered seemingly at random!
I've since discovered that Listview needs to have an extended view class that allows the values at that position to be stored.
I need to pass several values, so far all the examples I have seen only show one method with one values per cell (referring to http://commonsware.com/Android/excerpt.pdf), I am seeking assistance on how to store the values for my application.
I have a "total" count , and a separate count that I can increase and decrease the value of, via buttons within the listview cell. I need to store these individual totals and values within that position in the View. I supposed I would have a method/constructor that simply includes those values, but in this syntax is seems the super. will not allow this override.
Insight Appreciated. If this wasn't clear without code, sure just let me know. But if you are already familiar with this problem and the most generic way to tackle it, that would be great too.
Listview recycles its views, so if you don't store the values you are displaying explicitly using a custom adapter fit for your purposes, that is probably were it goes wrong. Whenever you scroll a view in the listview out of the screen, it throws away all the values in there, and pastes it on top with new data.
So what you probably need is a custom adapter storing the numbers you want to display; here is a good place to start off from in understanding it better: http://developer.android.com/videos/index.html#v=wDBM6wVEO70
Related
I am using 'Codeofaninja' .
android table scroll code
He generates data. I replaced that with an sqlite database. It works well but...
I need to sort and the displays are textviews with tablerows.
Should I use listviews instead of the textviews/tablerows?
I have seen examples of data being sorted in a collection. I have my data in lists already but I have read that textviews have performance problems.
If the answer is listviews then I have to redesign the views which I am trying to not do. But if technology says I must then so be it.
So I have come up with 2 options:
1:textview gets repopulated with list after any data actions.
2:listview is where data is manipulated then stored back to db. Then I need to put a listview in the relativelayout view?
I have tried deleting the tablerows from the textview and reading data back in but this proves slow.
I searched on textviews and listviews and have seen many examples but it is still not clear as to what method is the preferred.
Thank you for input.
The idea is that the sorting is independent of the view. You sort the data in the collection (list, array, etc.) first, then use the ListAdapter (or ArrayAdapter, .etc) to populate the view.
From what you described, it seems the textviews are re-created every time, i.e, if you have 10 rows, each row has 3 textviews, did you create 30 textviews? In that case, sure it has performance problem. Try reading up on ViewHolder for ListView
Android Viewholder implementation
It is superior to use the idea of loading some data at a time. Both Android ListView and RecyclerView virtually loads data when required, and removes data when they are no longer needed.
One good tutorial about ListView # Populating a ListView With Data. Tell us what you think of it.
The only drawback for these GUI classes is when you only have small amount of data to show, which is not likely, from your post.
I get data from my server returned as an array of objects. Each object is itself an array of strings that describe the object.
For example let's use cars as the object. In this case, the array of strings are descriptors like 'year built', 'horsepower', 'automatic or manual', 'color', etc etc.
What I'd like to do is display only 1 car at a time for the user. The user can choose whether he likes or dislikes the car. Either way, a choice is final, and the next car will show up. Also, the user should not be able to go back to the previous car (not with a swipe or a clicking of aback button). In other words, he can never see his choice on the previous car again.
If I make a call to my server on every single 'like/dislike', this will be a very slow app. If I inflate a bunch of data into many views that are out of sight, it will also be a very slow app.
Most Array Adapter examples I see online illustrate how to show data bi-directionally. They are viewpagers or listviews that you can swipe left (or up) to view data that has been previously already viewed. This is not what I'm looking for.
Is there a proper way to implement what I'm trying to do? Any help will be appreciated as I'm just hoping to get some direction and can implement on my own. So far I'm thinking about possibly a viewpager that deletes items as you view them, but I have a feeling this will be really hard to manage the position of the views...
Use an array to hold the data you get from the server. You can request that the server send you 10 or 25 or 50 at a time.
Since you only want to show the user 1 at a time, and he can't scroll or swipe through the list, you only need a single set of views (enough to show all attributes of a single entry). You don't need an array adpater for this. Whenever you step from one entry to the next you just need to adjust the index into your array and then copy all the attributes of the new item into the individual views with setText() or setImageBitmap() or whatever.
If you are creating a very dynamic list, say, where every row can have a different set of input types plus optional buttons, and the list length is based on another dynamic value, is it better to do this in a list adapter or creating a custom view in a scroll window?
After struggling with list adapters for quite a while now something finally occurred to me- this seems dumb. It seems like I am going through a lot of work keeping track of what spinner is set to what value, which row was clicked and so forth.
For example, say you are showing something like a contacts screen with various details that can be entered about a contact. Some rows will have text inputs (name, address etc), some will have spinners (ie. state, group), some will have checkboxes (like 'favorite' or something). Also, there is an 'add' button that allows you to add another field to edit. Is it worth making this in a list adapter or is it better to populate a custom view, and if the "add" button is clicked, we re-create the custom view, adding a view of the type they want to add?
I hope this is clear.
ListViews (and List Adapters) are meant for data that is to be displayed in mainly similar views. For your example, it is much easier and more natural to have a predefined layout file with the screen and use view visibility so select which views are to be shown. If you need to add views to the screen you can do this dynamically by using findViewById on the layout and then using it's addView method.
Let me know if you need more clarification or sample code...
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'm making an app in which the user can log simple timesheet-like transactions into a list. I have done this with a listview which is connected to a sqlite query with a custom CursorAdapter descendent. So far it is working well.
What I am trying to do next is let the user add any number of "tags" to an individual transcation (think labels against emails in gmail). I have a many-many-relationship table to link tags to transactions, but i'm unsure how to modify my existing listview and cursor arrangement so that each individual record can load and display its tags.
One thing that comes to mind is to load all the data into a two-tiered object structure, then use that as the datasource for the listview. I don't like this idea though because it will certainly cause a delay in loading, whereas using a CursorAdapter seems to be better for performance.
The way I was intending to display the tags within each listview item was just as plain text, linearly along the bottom of the row with different colours using Html.fromHtml(). I already have this part working so i was hoping to stick with the existing listview (if possible). My problem is purely about finding the best and most efficient way of getting the tags out of the DB and making them available to each row in the listview.
I'm very new to android - is there a clever way to do this or is it going to be a manual exercise?
There's no shortcut for this. You're going to have to create a View by subclassing LinearLayout, RelativeLayout or whatever you want. In your View class expose a public method like 'setTag(tag)' which takes an argument as to what tag to set the corresponding data to. In your View you'll have to have some field which maintains a reference to that View. In other words, somewhere before you return your view in getView in your Adapter, you can bind the data to the View and then alter its state from within the View.