0
我有一个需要4个参数的类。这个类叫做Item。我创建了一个ArrayList的List变量。然后我创建一个新的ListAdapter。我将此适配器设置为主ListView。然后我将10个输入添加到ArrayList中。这一切都工作,但当我尝试将ArrayList项目添加到ListAdapter是给我一个错误说要添加Cast到方法。将列表添加到自定义适配器时出错 - Android
这里是我讲的
final List <Item> tempList = new ArrayList <Item>();
mainListViewListAdapter = new ListAdapter(MainActivity.this, R.layout.main_list_item_layout, tempList);
// set mainListAdapter
mainListView.setAdapter(mainListViewListAdapter);
for (int x = 0; x < 10;x++){
tempList.add (new Item ("Example1","Example2","Example3","Example4"));
}
***HERE IS WHERE I GET THE ERROR AT addAll();***
mainListViewListAdapter.addAll(tempList);
mainListViewListAdapter设置为ListAdapter的代码,并mainListView设置为ListView控件。
为什么要添加转换到mainListViewListAdapter?我已经在另一个项目上运行这个代码,我不必使用强制转换。
为了避免这种错误,您可以设置适配器之前,添加项目。使用addAll方法删除行,如果用这种方式完全不重要。 – lub0v
好主意,但我需要在设置适配器后添加它们,因为项目在活动期间被多次添加到listadapter – user2861085
我想出了答案。 'ListAdapter'需要在mainListViewListAdapter = new ListAdapter(MainActivity.this,R.layout.main_list_item_layout,tempList)之前;' – user2861085