android 将多个适配器附加到一个适配器
// Declarations
private SimpleAdapter _resultsAdapter;
private ArrayAdapter<String> _adapter;
private List<Map<String,?>> _resultsList;
private ArrayList<String> _stringList = new ArrayList<String>();
// Much of source code here
// The following lines work (I can addSection()).
_adapter = new ArrayAdapter<String>(this, R.layout.custom_list_item, _stringList);
_sla = new SeparatedListAdapter(this);
_sla.addSection("Input Data", _adapter);
// More source code here...
// The following causes a crash
_resultsList.add(createItem(resultTitle.toString(), fieldDetails.toString())); // Loading data in a loop (works 100%)
_resultsAdapter = new SimpleAdapter(CompanyInfoServiceViewActivity.this, _resultsList, R.layout.list_complex, new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption });
_sla.addSection("Results", _resultsAdapter); // Crashes here. _sla is not null (see above)
I have been using the SeparatedListAdapter which is very well known and works perfectly, but it seems that I cannot use the addSection() to add a SimpleAdapter, because the application is terminated. I'm providing some code to show you what I'm trying to do and get some guidance in order solve this issue. Please let me know if you need any other piece of code or anything:
// Declarations
private SimpleAdapter _resultsAdapter;
private ArrayAdapter<String> _adapter;
private List<Map<String,?>> _resultsList;
private ArrayList<String> _stringList = new ArrayList<String>();
// Much of source code here
// The following lines work (I can addSection()).
_adapter = new ArrayAdapter<String>(this, R.layout.custom_list_item, _stringList);
_sla = new SeparatedListAdapter(this);
_sla.addSection("Input Data", _adapter);
// More source code here...
// The following causes a crash
_resultsList.add(createItem(resultTitle.toString(), fieldDetails.toString())); // Loading data in a loop (works 100%)
_resultsAdapter = new SimpleAdapter(CompanyInfoServiceViewActivity.this, _resultsList, R.layout.list_complex, new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption });
_sla.addSection("Results", _resultsAdapter); // Crashes here. _sla is not null (see above)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将 MergeAdapter 用于 ListView。这是我修改并经过全面测试的版本。
如果需要,您还需要 ListTitleAdapter 在每个适配器之前放置一些标题。
这是有关如何使用这两个类的小示例。
You can use MergeAdapter for your ListView. This is my modified and fully tested version.
You also will need ListTitleAdapter to put some titles before each adapter if you want.
And here is small example on how to use these two classes.
添加了这一行:
在该行下方:
Added this line:
below the line: