求助ListView一个简单例子
第一,TestListView.java
package com.cellon.bo;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class TestListView extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.id.list);
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> map1 = new HashMap<String, String>();
HashMap<String,String> map2 = new HashMap<String, String>();
HashMap<String,String> map3 = new HashMap<String, String>();
map1.put("name", "libo");
map1.put("age","23");
map2.put("name", "min");
map2.put("age", "24");
map3.put("nage", "liping");
map3.put("age", "21");
list.add(map1);
list.add(map2);
list.add(map3);
SimpleAdapter simpleAdapter = new SimpleAdapter(TestListView.this, list, R.layout.user, new String[]{"name","age"}, new int[]{R.id.user_name,R.id.user_age});
setListAdapter(simpleAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
}
}
第二,main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:drawSelectorOnTop="false"
></ListView>
</LinearLayout>
第三,user.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/user_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
第四,错误提示
E/AndroidRuntime(883): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cellon.bo/com.cellon.bo.TestListView}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
疑问:
这是怎么回事?运行不了。 listview hashmap android
[解决办法]
把listview 的ID改成 id=android:list
还有你的Map最好放在配置文件中,你写的代码不推荐。
