标签:
1.ListView之简单使用
package com.zrf.demo; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class MyListViewDemo extends Activity { private String[] datas = { "php", "jsp", "asp", "java", "c++", "c", "javascript", "html", "css", "jQuery", "jQuery EasyUI", "BootStrap", "Extjs", "Ajax" }; private ListView lsitView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.lsitView = new ListView(this);// 实例化ListView this.lsitView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.datas));//为ListView组建设置内容 super.setContentView(this.lsitView);// 显示组件 } } |
更改显示风格
package com.zrf.demo; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class MyListViewDemo extends Activity { private String[] datas = { "php", "jsp", "asp", "java", "c++", "c", "javascript", "html", "css", "jQuery", "jQuery EasyUI", "BootStrap", "Extjs", "Ajax" }; private ListView lsitView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.lsitView = new ListView(this);// 实例化ListView this.lsitView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, this.datas));//为ListView组建设置内容 super.setContentView(this.lsitView);// 显示组件 } } |
2.SimpleAdater之使用
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableRow> <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/file_icon"/> <TextView android:id="@+id/_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20px" android:gravity="center_horizontal"/> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20px" android:gravity="center_horizontal"/> </TableRow> </TableLayout> |
<?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:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="25px" android:gravity="center_horizontal" android:text="编程语言列表" /> <ListView android:id="@+id/datalsit" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> |
package com.zrf.demo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; public class MyListViewDemo extends Activity { private String[][] datas = { { "001", "php" }, { "002", "jsp" }, { "003", "asp" }, { "004", "java" }, { "005", "c++" }, { "006", "c" }, { "007", "javascript" }, { "008", "html" }, { "009", "css" }, { "010", "jQuery" }, { "011", "jQuery EasyUI" }, { "012", "BootStrap" }, { "013", "Extjs" }, { "014", "Ajax" } }; private ListView dataList = null;// 定义ListView组件 private List<Map<String, String>> list = new ArrayList<Map<String, String>>();// 定义显示的内容包装 private SimpleAdapter simpleAdapter = null;// 进行数据的转换操作 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); this.dataList = (ListView) super.findViewById(R.id.datalsit);// 取得组件 for (int x = 0; x < datas.length; x++) { Map<String, String> map = new HashMap<String, String>();// 定义Map集合,保存每一行数据 map.put("_id", this.datas[x][0]);// 与date_list.xml中的TextView组加匹配 map.put("name", this.datas[x][1]);// 与date_list.xml中的TextView组加匹配 this.list.add(map); } this.simpleAdapter = new SimpleAdapter(this, this.list, R.layout.data_list, new String[] { "_id", "name" }, new int[] { R.id._id, R.id.name });//// 是data_list.xml中定义的组件的资源ID this.dataList.setAdapter(this.simpleAdapter); } } |
标签:
原文地址:http://www.cnblogs.com/codestyle/p/5433345.html