今天学习了ListView控件和页面跳转,下面大致介绍下:
第一步:创建显示内容的文件vlist.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="wrap_content" android:orientation="horizontal" android:background="#31B6E7" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px" /> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/txtid" android:layout_width="0dp" android:layout_height="0dp"/> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:textSize="22px"/> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:textSize="13px"/> </LinearLayout> </LinearLayout>
下面是主Activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/txtDescription" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <LinearLayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_weight="1" > </ListView> </LinearLayout> </RelativeLayout>
下面是对应的代码:
private void bindData() { ListView lv = (ListView)findViewById(R.id.listView1); SimpleAdapter adapter= new SimpleAdapter(this, getData(), R.layout.vlist, new St ring[]{"id","img","title","info"}, new int[]{R.id.txtid, R.id.img,R.id.title,R.id.info}); lv.setAdapter(adapter); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub ListView listView = (ListView)arg0; HashMap<String, String> map = (HashMap<String, String>)listView.getItemAtPosition(arg2); String id = map.get("id"); String info = map.get("info"); TextView txtDescription = (TextView)findViewById(R.id.txtDescription); StringBuilder sb = new StringBuilder(); sb.append(getResources().getText(R.string.detail)); sb.append(":"+info); sb.append("-----id:"+id); txtDescription.setText(sb); } }); }
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = null;
for (int i = 0; i < 10; i++) {
map = new HashMap<String, Object>();
map.put("id", i);
map.put("title", "Title");
map.put("info", "google "+(i+1));
map.put("img", R.drawable.ic_action_search);
list.add(map);
}
return list;
}
上面即ListView的呈现和Click Item 的使用方法