我有一个获取远程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
答案 0 :(得分:0)
我找到了此网站的解决方案: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);
}
}