简体   繁体   English

使用 ArrayAdapter 创建多列布局

[英]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).我是 Android 的初学者,但了解大部分基础知识(我认为)。 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.我为此使用 ListView,因为我需要用户能够单击一行以更详细地查看数据,因为只有这么多可以放在一行中。 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.我使用 SimpleAdapter 让它完美地工作,但由于数据量有时可能很大,最多 500 个条目,我想切换到延迟加载器系统,这样用户就不必盯着黑屏 20 秒当一切都加载时。 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.我想我可以使用 Lazy Loader 部分进行管理,但我不知道如何从我的多列格式 SimpleAdapter 切换到所有 Lazy Loader 示例使用的 ArrayAdapter。

Here's an example of my SimpleAdapter code:这是我的 SimpleAdapter 代码的示例:

SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to); SimpleAdapter 适配器 = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);

fillMaps is the List of HashMaps that are to be inserted. fillMaps 是要插入的 HashMap 列表。 'grid_item' is the layout that the entries are inserted into. 'grid_item' 是条目插入的布局。 'from' is an array of column names (alternatively the keys in the HashMap) to be used. 'from' 是要使用的列名数组(或者 HashMap 中的键)。 'to' is an array of TextView items that will be filled in 'grid_item'. 'to' 是一个由 TextView 项目组成的数组,它们将被填充到 'grid_item' 中。 Any idea how to convert this to an ArrayAdapter?知道如何将其转换为 ArrayAdapter 吗? Or how to Lazy Load with a SimpleAdapter?或者如何使用 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创建了我自己的名为 Entry 的 class ,它只包含行的字符串
  • Created a custom adapter that extends ArrayAdapter.创建了一个扩展 ArrayAdapter 的自定义适配器。 For the constructor I pass into it an ArrayList of type Entry.对于构造函数,我将一个条目类型的 ArrayList 传递给它。
  • Create a ViewHolder for better UI efficiency创建 ViewHolder 以提高 UI 效率
  • Inflate the row layout xml which is then added to my listview膨胀行布局 xml 然后添加到我的列表视图

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在我的自定义适配器中添加了一个方法,它将 append 一个新的条目对象 ArrayList 到我已经在我的适配器中的 ArrayList
  • 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创建了一个 onScrollListener 并从一些调试测试中发现,如果顶部可见项加上剩余条目等于总条目,那么我一直滚动到底部
  • 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一旦检测到底部,我会调用我的 fetch 方法来检索另外 30 个条目,使用我创建的 add 方法添加这些条目,然后使用 ArrayAdapter 的 notifyDataSetChanged() 方法来显示新加载的项目

Just write your own adapter.只需编写自己的适配器。 Create a private a class in your Activity that houses your ListView that extends BaseAdapter .在您的Activity中创建一个私有的 class ,其中包含扩展BaseAdapterListView 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. SimpleAdapter显然很有用,你会从你自己的适配器中获得更多的牵引力,而且它们真的不难编写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM