Creating Multi-Column Layout with ArrayAdapter - android

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.

Related

Using SimpleCursorAdapter and ArrayAdapter in a CustomAdapter

I have a ListView, the first 5 elements of which is to be populated by elements retrieved from an array defined in the strings.xml. These are static and will never change. The rest of the elements will be populated from the SQLite DB. These are dynamic and the user can remove from and add to these.
Is there a way I can implement a custom adapter where I can use a combination of ArrayAdapter and SimpleCursorAdapter.
I tried searching online for such a combination, but could not find anything that could help. Any help is appreciated.

Complex Items in a Listview

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.

Android: How to create CursorAdapter from the data in an XML file

In android can we create CursorAdapter from the data in an XML file? Here's the actual problem:
I need to create a ListView whose items can vary. Each items has three fields: ItemName, ItemValue, ItemUnit. If the adapter is created from an external file (eg: an XML file) provided to the application, then the number of items and it's field value can change without changing the application code. What is the best possible way to achieve this?
You could just load the data from the xml file into a list of Maps and then use a SimpleAdapter. That's probably the easiest thing to do. Note that you'll have to give the SimpleAdapter a mutable map and manually call notifyDataSetChanged whenever stuff changes as described here. If you're list isn't to big (less than 1000 items), you could probably get away with just creating a new adapter every time your data changes and then assign the new adapter to your list.

Confused when to use Adapters and which one?

I have read a little bit about ListAdapter - ArrayAdapter - BaseAdapter - CursorAdapter. I don’t understand there usage in true sense.
I have scenario, in which I am showing word in a TextView and then there are 4 radio button options and user will select one.
I am confused if I can use any adapter functionality. Like when the word on the top is move to next word. Meaning selection shown as radio button options updates automatically because of binding.
In a nut shell i am looking for something like auto binding in .NET.
BaseAdapter is the most basic Adapter of ListView. All remaining adapters extend from BaseAdapter.
If you are confused which adapter is suitable for you scenario, let choose BaseAdapter first.
ArrayAdapter can be used to link say a list of items with an array. The ArrayAdapter works between an your array data and a list item layout to populate a list.
a CursorAdapter can work in a similar way but can link a database query result set (in a cursor) to a list by populating list items.
They are both subclasses of ListAdapter
If data changes in either the database tables or Array both the Array and Cursor Adapters can be refreshed via notifyDataSetChanged()

How to reuse one custom listview activity in another activity?

Can I use my existing ListView as a sub-view in another view.
eg. I have a custom view, players list, I have implemented with a ListActivity and ArrayAdapter and is working fine.
Now I want a way to get this listview as View object so that I can add this view object as a child to another view.
I am thinking like, to build entire listview: my ListActivity is calling the ArrayAdapter iteratively by passing ArrayList item each time to build a list item view.
If I am correct, I need a way to call the same ArrayAdapter and need to prepare a ListView Object, right?
Can anybody plz help me.
Thanks in advance.
vpapana
The ListView and the Adapter are two different things:
The data is somewhere (in an array or if you need to be able to add/remove items, in an ArrayList)
The Adapter contains the data
The ListView displays it.
So:
Construct your empty ArrayList
Construct your adapter based on your ArrayList
Construct your ListView based on the Adapter
Then:
Each time you need to change the data, update the ArrayList
Call Adapter.notifyDataSetChanged()
Watch your list being automatically updated :)
To add your list to a tablelayout, see the API documentation, SDK tutorial and this tutorial which has a specific section on how to add rows programmatically.

Categories

Resources