1. ListView with Icon
(1)效果图
(2)实现代码
1)listview.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ListView android:id="@+id/listview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
2)simple_list_item_icon.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <ImageView android:id="@+id/simple_list_item_icon_iv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:baselineAlignBottom="true">
- </ImageView>
- <TextView android:id="@+id/simple_list_item_icon_tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="24dip">
- </TextView>
- </LinearLayout>
3)IconSimpleAdapter.java
- public class IconSimpleAdapter extends BaseAdapter {
- private LayoutInflater mInflater;
- private List<Map<String, Object>> mList;
- private String[] mIndex;
- public IconSimpleAdapter(Context context, String[] index, List<Map<String, Object>> list) {
- mInflater = LayoutInflater.from(context);
- mList = list;
- mIndex = index;
- }
- @Override
- public int getCount() {
- return mList.size();
- }
- @Override
- public Object getItem(int position) {
- return mList.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder = null;
- if (convertView == null) {
- Bitmap mIcon = (Bitmap) mList.get(position).get(mIndex[0]);
- String mItem = " " + (String) mList.get(position).get(mIndex[1]);
- convertView = mInflater.inflate(R.layout.simple_list_item_icon, null);
- holder = new ViewHolder();
- holder.text = (TextView) convertView.findViewById(R.id.simple_list_item_icon_tv);
- holder.icon = (ImageView) convertView.findViewById(R.id.simple_list_item_icon_iv);
- holder.text.setText(mItem);
- holder.icon.setImageBitmap(mIcon);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- return convertView;
- }
- private class ViewHolder {
- TextView text;
- ImageView icon;
- }
- }
4)IconListActivity.java
- public class IconListActivity extends Activity {
- private final String ITEM_ICON = "icon";
- private final String ITEM_TEXT = "text";
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.listview);
- List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
- Map<String, Object> item = new HashMap<String, Object>();
- Bitmap bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.a);
- item.put(ITEM_ICON, bm1);
- item.put(ITEM_TEXT, "中秋节");
- data.add(item);
- item = new HashMap<String, Object>();
- Bitmap bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.b);
- item.put(ITEM_ICON, bm2);
- item.put(ITEM_TEXT, "端午节");
- data.add(item);
- item = new HashMap<String, Object>();
- Bitmap bm3 = BitmapFactory.decodeResource(getResources(), R.drawable.c);
- item.put(ITEM_ICON, bm3);
- item.put(ITEM_TEXT, "重阳节");
- data.add(item);
- item = new HashMap<String, Object>();
- Bitmap bm4 = BitmapFactory.decodeResource(getResources(), R.drawable.d);
- item.put(ITEM_ICON, bm4);
- item.put(ITEM_TEXT, "清明节");
- data.add(item);
- ListView lv = (ListView) findViewById(R.id.listview);
- IconSimpleAdapter adapter = new IconSimpleAdapter(this, new String[] {ITEM_ICON, ITEM_TEXT}, data);
- lv.setAdapter(adapter);
- }
- }
2. ListView with 3Rows
(1)效果图
(2)实现代码
1)hs_activity.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@+id/hs_linearlayout">
- <ListView android:id="@+id/hs_activity_lv"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
- </LinearLayout>
2)simple_list_item_3.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:id="@+id/simple_list_item_3_tv1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="10dip"/>
- <TextView android:id="@+id/simple_list_item_3_tv2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="18dip"/>
- <TextView android:id="@+id/simple_list_item_3_tv3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="12dip"/>
- </LinearLayout>
3)ThreeRowsListActivity.java
- public class ThreeRowsListActivity extends Activity {
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.hs_activity);
- List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
- Map<String, Object> item = new HashMap<String, Object>();
- item.put("category", "便民,超市");
- item.put("name", "中百超市");
- item.put("address", "武汉街道口2547号");
- data.add(item);
- item = new HashMap<String, Object>();
- item.put("category", "五星级酒店");
- item.put("name", "希尔顿大酒店");
- item.put("address", "上海宝山路3651号");
- data.add(item);
- item = new HashMap<String, Object>();
- item.put("category", "写字楼,办公");
- item.put("name", "中环假日酒店");
- item.put("address", "北京菜户营1号");
- data.add(item);
- ListView listView = (ListView) findViewById(R.id.hs_activity_lv);
- ListAdapter adapter = new SimpleAdapter(this, data, R.layout.simple_list_item_3,
- new String[] {"category", "name", "address"},
- new int[] {R.id.simple_list_item_3_tv1, R.id.simple_list_item_3_tv2, R.id.simple_list_item_3_tv3});
- listView.setAdapter(adapter);
- }
- }
3. ListView with Icon
(1)How to get android default icons
1)http://www.darshancomputing.com/android/1.5-drawables.html
2)android.jar
(2)Classic Code
1)main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ListView android:id="@id/android:list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:drawSelectorOnTop="false"
- />
- </LinearLayout>
2)row.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- 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">
- <ImageView android:id="@+id/img"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView android:id="@+id/text1"
- android:textSize="12sp"
- android:textStyle="bold"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <TextView android:id="@+id/text2"
- android:textSize="12sp"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- </LinearLayout>
3)ResourceExplorer.java
- public class ResourceExplorer extends ListActivity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // define the list which holds the information of the list
- List<Map<String, Object>> resourceNames = new ArrayList<Map<String, Object>>();
- // define the map which will hold the information for each row
- Map<String, Object> data;
- // hard coded numbers retrieved from
- // http://code.google.com/android/reference/android/R.drawable.html
- for (int idx = 17301504; idx <= 17301655; idx++) {
- data = new HashMap<String, Object>();
- try {
- String stg = Resources.getSystem().getResourceName(idx);
- data.put("line1", stg);
- data.put("line2", idx);
- data.put("img", idx);
- resourceNames.add(data);
- } catch (Resources.NotFoundException nfe) {
- }
- }
- SimpleAdapter notes = new SimpleAdapter(this, resourceNames,
- R.layout.row, new String[] { "line1", "line2", "img" },
- new int[] { R.id.text1, R.id.text2, R.id.img });
- setListAdapter(notes);
- }
- }