0

リモート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
4

1 に答える 1

0

このサイトから解決策を見つけました:http://eureka.ykyuen.info/2010/01/03/android-simple-listview-using-simplleadapter/

これは私が見つけたものです:

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

}

于 2012-10-24T15:45:49.907 に答える