Android基础教程:使用ListView显示信息


以显示注册信息为例:

一、创建带有ListView的Activity要继承ListActivity

this.setContentView(R.layout.user);

二、ListActivity的布局文件user.xml

<ListView

Android:id="@id/android:list"

andoid:scrollbars="vertical"

android:drawSelectorOnTop="false">

三、使用intent获得用户保存的信息

Intent intent = getIntent();

String username = intent.getStringExtra("username");

 


四 、创建要显示的list内容

HashMap<String,String> map1 = new HashMap<String,String>();

..

map1.put("user_key","姓名");

map 1.put("user_value",username);

...

//对数组进行拆分

String hob = "";

for(String h:hobby){

hob += h=" ";

}

List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

list.add(map1);

...

五、设置适配器

设置list.xml布局文件;

指定list控件

SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.list,

new String[]{"user_key","user_value"},

new int[]{R.id.userkey,R.id.uservlaue}

);

setListAdapter(adapter);

相关内容