2015-07-20 128 views
0

我有2个适配器,其中每个适配器都包含不同的API,我想将其放入一个列表视图。我如何设置第二个适配器而不更换第一个适配器?前:)Android将多个适配器设置为一个列表视图

pcAPI = (ArrayList<ListAPI>) parsepcAPI(pcTemp); 
artistAPI = (ArrayList<ListAPI>) parseArtistAPI(artistTemp); 

lvAPI = (ListView) findViewById(R.id.lvAPI); 

Song_APIAdapter adapter = new Song_APIAdapter(getApplicationContext(), pcAPI); 
lvAPI.setAdapter(adapter); 

Song_APIAdapter adapter2 = new Song_APIAdapter(getApplicationContext(), artistAPI); 
lvAPI.setAdapter(adapter2); 
+0

你能描述哪些适配器的内容可能有解决方案吗? – Raghunandan

+0

https://github.com/commonsguy/cwac-merge – Renjith

+0

在pcAPI和artistAPI中添加项目并调用'adapter.notifyDataSetChanged();' –

回答

0

由于尝试合并2个适配器插入这样的单个适配器:

android attaching multiple adapters to one adapter

或者,你可以在两个ArrayLists合并成一个,然后设置Adapter。有效地,这就是你在做什么。

或者您可以先将pcAPI设置到适配器中,然后将artistAPI添加到pcAPI,然后再次设置新的适配器。并致电adapter.notifyDataSetChanged()

为什么你不能简单地做到这一点?

pcAPI = (ArrayList<ListAPI>) parsepcAPI(pcTemp); 
artistAPI = (ArrayList<ListAPI>) parseArtistAPI(artistTemp); 

ArrayList<ListAPI> mergedList = new ArrayList<ListAPI>(); 
mergedList.addAll(pcAPI); 
mergedList.add(artistAPI); 

Song_APIAdapter adapter = new Song_APIAdapter(getApplicationContext(), mergedAPI); 
lvAPI.setAdapter(adapter); 
+0

非常感谢你的工作:) 我是新来的机器人,经常会感到困惑哈哈 –

+0

我很高兴它做到了。如果解决了您的问题,您可以将答案标记为已接受。这也有助于未来的用户。谢谢。 –

1

试试这个代码将合并适配器

Song_APIAdapter adapter = new Song_APIAdapter(getApplicationContext(), pcAPI); 
    lvAPI.setAdapter(adapter); 
    Song_APIAdapter adapter2 = new Song_APIAdapter(getApplicationContext(), artistAPI); 
    adapter.addAdapter(adapter2); 
0

通常情况下,你不应该使用2个适配器1名列表视图。 Adatper是将数据转换为视图的界面。

就你而言,你的listview显示2种数据。所以你应该创建一个全新的适配器,它从pcAPI和artistAPI读取数据,然后生成一个新视图并将其传递给ListView。

如果你只需要显示pcView和artistView交替,有一个简单的方法:

  1. 创建一个新的适配器,并通过适配器和适配器2到它。

  2. override getCount(),getCount = adapter.getCount()+ adapter2.getCount();

  3. 重写getView,对于奇数项返回adapter.getView(...),对于偶数项返回adapter2.getCount();

我不知道你想要什么样的观点,所以我只是猜测你的要求。

相关问题