记录两个ListView的简单小例子.其中使用到 了SimpleAdapter适配器。
1、不带图片
listview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="1dip" android:paddingBottom="1dip"> <TextView android:id="@+id/name" android:layout_width="180dip" android:layout_height="30dip" android:textSize="10pt" android:singleLine="true" /> <TextView android:id="@+id/phone" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="right" android:textSize="10pt" /> </LinearLayout>
ListViewActivity.java
public class ListViewActivity extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 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", "张三"); map1.put("phone", "13800138000"); map2.put("name", "李四"); map2.put("phone", "13800138022"); map3.put("name", "王武"); map3.put("phone", "13800138033"); list.add(map1); list.add(map2); list.add(map3); SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.info, new String[]{"name","phone"},new int[]{R.id.name,R.id.phone}); setListAdapter(adapter); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Toast.makeText(ListViewActivity.this, ((HashMap)l.getItemAtPosition(position)).get("name").toString(), Toast.LENGTH_LONG).show(); }
运行效果:
2、带有图片效果:
listviewtwo.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/image" android:layout_width="30px" android:layout_height="30px" android:layout_margin="5px"/> <LinearLayout android:id="@+id/linerlayout" android:layout_width="wrap_content" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_vertical"> <TextView android:id="@+id/name1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20px"/> <TextView android:id="@+id/phone1" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textSize="10px"/> </LinearLayout> </LinearLayout>
ListViewTwoActivity.java
public class ListViewTwoActivity extends ListActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String,Object>>(); HashMap<String, Object> map1 = new HashMap<String, Object>(); HashMap<String, Object> map2 = new HashMap<String, Object>(); HashMap<String, Object> map3 = new HashMap<String, Object>(); map1.put("name", "张三"); map1.put("phone", "13800138000"); map1.put("image", R.drawable.icon); map2.put("name", "李四"); map2.put("phone", "13800138022"); map2.put("image", R.drawable.jinji1); map3.put("name", "王武"); map3.put("phone", "13800138033"); map3.put("image", R.drawable.jixugoucai); list.add(map1); list.add(map2); list.add(map3); list.add(map1); list.add(map2); list.add(map3); list.add(map1); list.add(map2); list.add(map3); list.add(map1); list.add(map2); list.add(map3); list.add(map1); list.add(map2); list.add(map3); list.add(map1); list.add(map2); list.add(map3); list.add(map1); list.add(map2); list.add(map3); SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.listviewtwoinfo, new String[]{"name","phone","image"},new int[]{R.id.name1,R.id.phone1,R.id.image}); setListAdapter(adapter); } }
运行效果: