Android:如何从XML文件中的数据创建CursorAdapter

时间:2021-07-16 09:54:57

In android can we create CursorAdapter from the data in an XML file? Here's the actual problem:

在android中我们可以从XML文件中的数据创建CursorAdapter吗?这是实际问题:

I need to create a ListView whose items can vary. Each items has three fields: ItemName, ItemValue, ItemUnit. If the adapter is created from an external file (eg: an XML file) provided to the application, then the number of items and it's field value can change without changing the application code. What is the best possible way to achieve this?

我需要创建一个ListView,其项目可以变化。每个项目都有三个字段:ItemName,ItemValue,ItemUnit。如果适配器是从提供给应用程序的外部文件(例如:XML文件)创建的,那么项目的数量及其字段值可以在不更改应用程序代码的情况下更改。实现这一目标的最佳方法是什么?

1 个解决方案

#1


1  

You could just load the data from the xml file into a list of Maps and then use a SimpleAdapter. That's probably the easiest thing to do. Note that you'll have to give the SimpleAdapter a mutable map and manually call notifyDataSetChanged whenever stuff changes as described here. If you're list isn't to big (less than 1000 items), you could probably get away with just creating a new adapter every time your data changes and then assign the new adapter to your list.

您只需将xml文件中的数据加载到Maps列表中,然后使用SimpleAdapter即可。这可能是最简单的事情。请注意,您必须为SimpleAdapter提供一个可变映射,并在每次更改内容时手动调用notifyDataSetChanged,如此处所述。如果您的列表不是很大(少于1000个项目),那么每次数据更改时只需创建一个新适配器,然后将新适配器分配给列表即可。

#1


1  

You could just load the data from the xml file into a list of Maps and then use a SimpleAdapter. That's probably the easiest thing to do. Note that you'll have to give the SimpleAdapter a mutable map and manually call notifyDataSetChanged whenever stuff changes as described here. If you're list isn't to big (less than 1000 items), you could probably get away with just creating a new adapter every time your data changes and then assign the new adapter to your list.

您只需将xml文件中的数据加载到Maps列表中,然后使用SimpleAdapter即可。这可能是最简单的事情。请注意,您必须为SimpleAdapter提供一个可变映射,并在每次更改内容时手动调用notifyDataSetChanged,如此处所述。如果您的列表不是很大(少于1000个项目),那么每次数据更改时只需创建一个新适配器,然后将新适配器分配给列表即可。