java 如何使用 XML 或 JSON 数据填充 ListView(在 Android 中)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2227154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I fill a ListView (in Android) with XML or JSON data?
提问by TIMEX
I read a tutorial, and it uses SQLlite and "SimpleCursorAdapter" to fill the list with items. This is the code the tutorial taught me.
我读了一个教程,它使用 SQLlite 和“SimpleCursorAdapter”来填充列表。这是教程教我的代码。
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = mDbHelper.fetchAllNotes();
startManagingCursor(c);
String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.text1 };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
However...what if I want to fill it with XML data? Is it the same method? Can someone give me an example (in code)? thanks.
但是...如果我想用 XML 数据填充它怎么办?是同样的方法吗?有人可以给我一个例子(在代码中)吗?谢谢。
回答by JeremyFromEarth
The example is using a CursorAdapterbecause a Cursorobject is returned by the NotesDbAdapter(if i remember correctly ) fetchAllNotesmethod. I don't know if there is a way to pass in raw XML to create a list but you can use name/value pairs in a HashMapto create a list using the SimplelistAdapter.
该示例使用 a 是CursorAdapter因为Cursor对象是由NotesDbAdapter(如果我没记错的话)fetchAllNotes方法返回的。我不知道是否有办法传入原始 XML 来创建列表,但您可以使用HashMapSimplelistAdapter 中的名称/值对来创建列表。
You can parse your xml and or json and build a hash table with it and use that to populate a list. The following example doesn't use xml, in fact it's not dynamic at all, but it does demonstrate how to assemble a list at runtime. It's taken from the onCreatemethod of an activity that extends ListActivity. The all uppercase values are static constant strings defined at the top of the class, and are used as the keys.
您可以解析您的 xml 和/或 json 并使用它构建一个哈希表并使用它来填充列表。下面的示例不使用 xml,实际上它根本不是动态的,但它确实演示了如何在运行时组装列表。它取自onCreate扩展的活动的方法ListActivity。所有大写的值都是在类的顶部定义的静态常量字符串,并用作键。
// -- container for all of our list items
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
// -- list item hash re-used
Map<String, String> group;
// -- create record
group = new HashMap<String, String>();
group.put( KEY_LABEL, getString( R.string.option_create ) );
group.put( KEY_HELP, getString( R.string.option_create_help ) );
group.put( KEY_ACTION, ACTION_CREATE_RECORD );
groupData.add(group);
// -- geo locate
group = new HashMap<String, String>();
group.put( KEY_LABEL, getString(R.string.option_geo_locate ) );
group.put( KEY_HELP, getString(R.string.option_geo_locate_help ) )
group.put( KEY_ACTION, ACTION_GEO_LOCATE );
groupData.add( group );
// -- take photo
group = new HashMap<String, String>();
group.put( KEY_LABEL, getString( R.string.option_take_photo ) );
group.put( KEY_HELP, getString(R.string.option_take_photo_help ) );
group.put( KEY_ACTION, ACTION_TAKE_PHOTO );
groupData.add( group );
// -- create an adapter, takes care of binding hash objects in our list to actual row views
SimpleAdapter adapter = new SimpleAdapter( this, groupData, android.R.layout.simple_list_item_2,
new String[] { KEY_LABEL, KEY_HELP },
new int[]{ android.R.id.text1, android.R.id.text2 } );
setListAdapter( adapter );

