繁体   English   中英

Android XML到自定义列表视图

[英]Android XML to Custom List VIew

我有一个程序可以获取远程xml文件并在android中创建列表视图,我可以从xml中提取数据,但似乎无法将其放在正确的位置。 我有一个自定义布局,但它不在正确的字段中。 我怎样才能做到这一点? 这是我的代码的一部分。 目前无法使用。 提前致谢

  // looping through all item nodes <item>
                for (int i = 0; i < nl.getLength(); i++) {
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) nl.item(i);
                // adding each child node to HashMap key => value
                map.put(KEY_ID, parser.getValue(e, KEY_ID));
                map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
                map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
                map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

                // adding HashList to ArrayList
                menuItems.add(map);
            }

            // Adding menuItems to ListView
            ListAdapter adapter = new SimpleAdapter(this, menuItems,R.layout.list_item,
                      new String[] { KEY_NAME, KEY_DESC, KEY_COST }, 
                      new int[] { R.id.status_data_user, R.id.status_data_msg, R.id.status_data_date });

错误如下:

10-23 21:33:55.379: W/dalvikvm(613): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-23 21:33:55.402: E/AndroidRuntime(613): FATAL EXCEPTION: main
10-23 21:33:55.402: E/AndroidRuntime(613): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vlad.xml.parser.app/com.vlad.xml.parser.app.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
10-23 21:33:55.402: E/AndroidRuntime(613):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-23 21:33:55.402: E/AndroidRuntime(613):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-23 21:33:55.402: E/AndroidRuntime(613):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-23 21:33:55.402: E/AndroidRuntime(613):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-23 21:33:55.402: E/AndroidRuntime(613):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 21:33:55.402: E/AndroidRuntime(613):  at android.os.Looper.loop(Looper.java:123)
10-23 21:33:55.402: E/AndroidRuntime(613):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-23 21:33:55.402: E/AndroidRuntime(613):  at java.lang.reflect.Method.invokeNative(Native Method)
10-23 21:33:55.402: E/AndroidRuntime(613):  at java.lang.reflect.Method.invoke(Method.java:521)
10-23 21:33:55.402: E/AndroidRuntime(613):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-23 21:33:55.402: E/AndroidRuntime(613):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-23 21:33:55.402: E/AndroidRuntime(613):  at dalvik.system.NativeStart.main(Native Method)
10-23 21:33:55.402: E/AndroidRuntime(613): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
10-23 21:33:55.402: E/AndroidRuntime(613):  at android.app.ListActivity.onContentChanged(ListActivity.java:245)
10-23 21:33:55.402: E/AndroidRuntime(613):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
10-23 21:33:55.402: E/AndroidRuntime(613):  at android.app.Activity.setContentView(Activity.java:1647)
10-23 21:33:55.402: E/AndroidRuntime(613):  at com.vlad.xml.parser.app.MainActivity.onCreate(MainActivity.java:37)
10-23 21:33:55.402: E/AndroidRuntime(613):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-23 21:33:55.402: E/AndroidRuntime(613):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-23 21:33:55.402: E/AndroidRuntime(613):  ... 11 more

我从以下站点找到了解决方案: http : //eureka.ykyuen.info/2010/01/03/android-simple-listview-using-simpleadapter/

这就是我发现的:

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ListView lv= (ListView)findViewById(R.id.listview);

    // create the grid item mapping
    String[] from = new String[] {"rowid", "col_1", "col_2"};
    int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3 };


    // prepare the list of all records
    List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
    for(int i = 0; i < 10; i++){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("rowid", "" + i);
        map.put("col_1", "col_1_item_" + i);
        map.put("col_2", "col_2_item_" + i);
        fillMaps.add(map);
    }

    // fill in the grid_item layout
    SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
    lv.setAdapter(adapter);
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM