如何填充一个ListView(Android中)与XML或JSON数据? [英] How do I fill a ListView (in Android) with XML or JSON data?

查看:92
本文介绍了如何填充一个ListView(Android中)与XML或JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读的教程,它使用SQLlite和SimpleCursorAdapter来填充列表项。 这是C教程中的$ C $教给我的。

I read a tutorial, and it uses SQLlite and "SimpleCursorAdapter" to fill the list with items. This is the code the tutorial taught me.

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);
    }

但是,如果我想使用XML数据填充它是什么?难道同样的方法?有人可以给我一个例子(在code)?谢谢。

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.

推荐答案

这个例子是使用的CursorAdapter ,因为光标对象由 NotesDbAdapter (如果我没记错的话) fetchAllNotes 方法返回。我不知道是否有一种方法可以通过在原始的XML创建一个列表,但您可以使用名称/值对在的HashMap 以创建使用SimplelistAdapter列表

The example is using a CursorAdapter because a Cursor object is returned by the NotesDbAdapter (if i remember correctly ) fetchAllNotes method. 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 HashMap to create a list using the SimplelistAdapter.

您可以分析您的XML和JSON或并建立一个哈希表,并用它来填充列表。下面的示例不使用XML,其实它不是动力可言,但它确实演示了如何组装一个列表在运行时。它取自的onCreate 扩展 ListActivity 活动的方法。全大写的值是在类的顶部定义静态常量字符串,被用作键。

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 onCreate method 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.

// -- 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 );

这篇关于如何填充一个ListView(Android中)与XML或JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆