开发者

Creating Multi-Column Layout with ArrayAdapter

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 t开发者_开发技巧o 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜