Hello
I am trying to build a listview based on content from the web.
I have had a look at :
http://androidboss.com/load-listview-in-background-asynctask/
example but it uses a predefined array of months. How can I substitute the known
array of months for an unknown undetermined number of items from the internet?
I want to load a listview with some data from the internet, the user
scrolls the list and it retrieves the next row(s) from the internet etc etc
rather than using a array of predetermined length.
Thanks Ian
You can use my EndlessAdapter for that. The project has a demo/ subproject demonstrating its use.
It sounds like you need to extend an adapter such as ArrayAdapter. Extending an ArrayAdapter so that you can dynamically generate the rows or alter the number of rows, and also notify the Adapter that the underlying data has changed, is a very common exercise in Android.
You'll find quite a few tutorials on this but, very basically, if you implement your own adapter by extending ArrayAdapter you can override getView() to programmatically generate each view, and you can override getCount() to provide the number of rows. You can use notifyDataSetChanged() to trigger a refresh of the list on the screen if some data has changed and you need to refresh.
Related
So I have a few arrays of data I would like to display in an activity without having like 15 text views with unique ids. Is there a code efficient way to make a Table layout or something like it where I could feed in data and it would automatically place it in there respective text views? Thanks!
I think you can achieve that by using RecyclerView (with a GridLayoutManager). Have a look at this answer.
If there are only TextViews and you don't want a specific layout you can use SimpleAdapter, if you want to modify the layout you have to extend RecycleView.Adapter (there is an example in the answer above).
You can add/remove items into/from a List and use DiffUtil that
can calculate the difference between two lists and output a list of update operations that converts the first list into the second one.
There are a lot of tutorials about using this class. Have a look here or here.
Or you can use the notifyItemChanged() method:
If the list needs an update, call a notification method on the
RecyclerView.Adapter object, such as notifyItemChanged(). The layout
manager then rebinds any affected view holders, allowing their data to
be updated.
LE: There are some libraries available. Here is a list:
https://github.com/evrencoskun/TableView
https://github.com/HYY-yu/TableRecyclerView
https://github.com/Cleveroad/AdaptiveTableLayout
https://github.com/celerysoft/TableFixHeaders
I have a ListView where the view for each item is a string (the name of the item). But I have to associate a lot of other data with that item: price, size, weight, tax, etc. So, I'm of creating a new Java class called Item, and then an object for each item in the list.
I don't know which is the bext way to implement this. There's two obvious choices:
1) I can simply create the data structure outside of any Android Views, and then write a method called UpdateList() which takes the name of each item in this data structure and puts it in the ListView. The problem with this is that some of the data is duplicated twice (the original data structure, and the adapter for the ListView) and when you duplicate data, bug potential arises.
2) Or, I can somehow associate the data structure directly with the adapter for the ListView and have it figure out how to display the name for each ListView entry that is displayed. The advantage here is that you only have a single data structure. But I don't know if this is possible in Android, or very complex.
Which is the preferred way to do this with Android apps?
You would be better with the ListView and the Adapter option, You would need to create a custom ArrayAdapter to populate a ListView from this objects the way you want.
The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.
In Short you would have to:
1. Create an object that represents your data for a single row.
2. Create an ArrayList of those objects.
3. Create a layout that contains a ListView or add a ListView to you main layout using code.
4. Create a layout of a single row.
5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.
6. Create a custom ArrayAdapter that will populate the rows according to you needs.
7. Finally assign this ArrayAdapter to your ListView in onCreate.
You can get an Idea of how to implement this by reading this blog post I wrote:
Create a Custom ArrayAdapter
Just use the adapter. It's much cleaner. Then you can retrieve the info you need when you display the list item with getView(). See this example.
Can anyone tell me what exactly does an array adapter do? I've tried searching the net but all I get is code examples. Please explain me what it does, I've visited the android developers as well.
An ArrayAdapter can be used as a data source for a number of different Android Views, such as a ListView or a Spinner.
Basically, you pass some kind of array or list to the constructor of an ArrayAdapter. Then, the adapter can be hooked up to a ListView by calling setAdapter(). You can also use the add and remove methods of the adapter to modify the underlying list itself.
You can also use an ArrayAdapter to customize the appearance of items in a ListView for example (or other Views) by using the constructor and passing in the resource of a layout to use, or by overriding the getView() method and building it yourself.
Typically, an adapter is some kind of translator. It's the "man in the middle" who know how to dialog with both sides and convert what is said.
An arrayAdapter is a class which get datas from an array and format it for a listview or spinner to understand it. When a listview need the data 4, for example, it will ask the adapter who will return him the 4 element of the array.
Ok, the listview could directly use the array.But with the adapter you're allowed to use any kind of data source. An ArrayAdapter(subclass of adapter) uses an Array, but another adapter could use a database or a file or anything else.That way the listview is able to get datas directly from any source without knowing how to access it.That is the adapter's role.
I've been trying to fix this problem for a while and have exhausted all the different Google searches that I could think of. I'm a beginner with Android, but understand most of the fundamentals (I think). Anyways, I'm trying to display a list of data in a multi-column layout spreadsheet style. I'm using a ListView for that because I need users to be able to click on a row to get a more detailed look at the data since only so much can fit in a row. I have it working perfectly using a SimpleAdapter, but since the amount of data can sometimes be large, up to 500 entries, I wanted to change over to a Lazy Loader system so users don't have to stare at a black screen for 20 seconds while everything loads. I think I can manage with the Lazy Loader part, but I can't figure out how to switch over from my multi-columned format SimpleAdapter to an ArrayAdapter that all the Lazy Loader examples use.
Here's an example of my SimpleAdapter code:
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
fillMaps is the List of HashMaps that are to be inserted. 'grid_item' is the layout that the entries are inserted into. 'from' is an array of column names (alternatively the keys in the HashMap) to be used. 'to' is an array of TextView items that will be filled in 'grid_item'. Any idea how to convert this to an ArrayAdapter? Or how to Lazy Load with a SimpleAdapter?
Thanks in advance!
To solve this issue for others, here is what I did to get it working perfectly:
Created my own class called Entry which simply contains the strings for the rows
Created a custom adapter that extends ArrayAdapter. For the constructor I pass into it an ArrayList of type Entry.
Create a ViewHolder for better UI efficiency
Inflate the row layout xml which is then added to my listview
To get the lazy loading working:
Added a method in my custom adapter that will append a new ArrayList of Entry objects to the ArrayList that I already have in my adapter
Created an onScrollListener and from some debugging test found that if the top visible item plus the remaining entries equalled the total entries, then I was scrolled all the way to the bottom
Once the bottom was detected, I would call my fetch method to retrieve another 30 entries, add these entries using the add method I created, and then use the notifyDataSetChanged() method for the ArrayAdapter to display the newly loaded items
Just write your own adapter. Create a private a class in your Activity that houses your ListView that extends BaseAdapter. Fill out the abstract methods and display your data however you want to. As useful as SimpleAdapter obviously is, you'll get a lot more traction out of your own adapter, and they're really not difficult to write.
I want to have a Button that when clicked, removes all the checked items in the ListView. I already have all the xml items set up, I just don't know how to write the java code.
The ListView displays data that comes from an Adapter. In order to remove items from the view the item needs to be removed from the Adapter and the view notified. In android the Adapter notifies the view by calling notifyDataSetChanged().
How to remove an item from the adapter depends on your particular adapter. The SimpleCursorAdapter gets its data from an underlying Cursor. To remove an item, the item should be removed from the underlying Cursor. For example using a SQLiteCursor a row in the database needs to be deleted.
If you use the ArrayAdapter just call remove(T object) on the adapter. It will automagically call notifyDataSetChanged() for you.
update:
I saw the code at git hub. Here are some pointers as how to get your app working as soon as possible.
Try refactoring your code in to smaller graspable parts. Start with extracting some methods to give parts of the large method understandable names.
The problem is that there might by hundreds of rows in the database and only enough views to fill the screen. Nowhere is it remebered what rows are checked, hence its not possible to remove them. You probably need to extend BaseAdapter or SimpleCursorAdapter to hold the state (checked or not) of the rows. Read up on the excellent android documentation.
My point here is there is a distinction between the view, your CheckBox, and the model containing the data to display. So check out Model-View-Controller. You can ignore the concept of controller for now.