今天又初步学习了一下ListView控件,看看效果如下:
LisViewActivity.java源码:
package com.jinhoward.UI_listview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class LisViewActivity extends Activity {
ListView listView=null;
Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lis_view);
listView =(ListView)findViewById(R.id.infolistview);
//生成动态数组,加入数据 ,添加的信息有string,也有int类型(图片id),所以用Map
List> list = new ArrayList>();
//一个map相当于listview的一行.
Map map = new HashMap();
map.put("name", "系统信息");
map.put("notes", "系统版本,运营商及其系统信息.");
map.put("img", R.drawable.system);//图片资源的id
list.add(map);
map = new HashMap();
map.put("name", "硬件信息");
map.put("notes", "CPU,硬盘,内存等硬件信息.");
map.put("img", R.drawable.hardware);
list.add(map);
map = new HashMap();
map.put("name", "软件信息");
map.put("notes", "已经安装的软件信息.");
map.put("img", R.drawable.software);
list.add(map);
map = new HashMap();
map.put("name", "运行时信息");
map.put("notes", "运行时的信息.");
map.put("img", R.drawable.running);
list.add(map);
map = new HashMap();
map.put("name", "文件浏览器");
map.put("notes", "文件系统.");
map.put("img", R.drawable.file_explorer);
list.add(map);
//生成适配器的Item和动态数组对应的元素
SimpleAdapter mySimpleAdapter = new SimpleAdapter(this, list, R.layout.item_row,
new String[] { "name", "notes", "img" }, new int[] { R.id.name,
R.id.notes, R.id.img });
//设置适配器
listView.setAdapter(mySimpleAdapter);
//设置监听器
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(LisViewActivity.this, "您选择了第"+(arg2+1)+"项", Toast.LENGTH_SHORT).show();
}
});
}
}
activity_lis_view.xml源码:
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#D1EEEE"
>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/infolistview" />
item_row.xml源码:
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vw1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="24dp"
android:layout_margin="4dp"
android:layout_height="24dp"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
android:textSize="12sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>