刚学 listview ,参考网上的教程
http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html
自己实现了一个 listview ,使用的是 SimpleAdapter ,稍微有点改动
代码如下:
public
class SListViewActivity
extends Activity {
/** Called when the activity is first created. */
private ListView iLv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iLv = (ListView) this.findViewById(R.id.lv);
SimpleAdapter ada = new SimpleAdapter( this,getData(),R.layout.vlist, new String[]{"title","info","img"},
new int[]{R.id.title,R.id.info,R.id.img});
iLv.setAdapter(ada);
}
private List<Map<String,Object>> getData()
{
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("title", "G1");
map.put("info","google");
map.put("img",R.drawable.i1);
list.add(map);
map = new HashMap<String,Object>();
map.put("title","G2");
map.put("info","google 2");
map.put("img", R.drawable.i1);
list.add(map);
return list;
}
/** Called when the activity is first created. */
private ListView iLv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iLv = (ListView) this.findViewById(R.id.lv);
SimpleAdapter ada = new SimpleAdapter( this,getData(),R.layout.vlist, new String[]{"title","info","img"},
new int[]{R.id.title,R.id.info,R.id.img});
iLv.setAdapter(ada);
}
private List<Map<String,Object>> getData()
{
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("title", "G1");
map.put("info","google");
map.put("img",R.drawable.i1);
list.add(map);
map = new HashMap<String,Object>();
map.put("title","G2");
map.put("info","google 2");
map.put("img", R.drawable.i1);
list.add(map);
return list;
}
------
xml 代码
main.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 ="fill_parent"
android:orientation ="vertical" >
< ListView
android:id ="@+id/lv"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
/>
</ LinearLayout >
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:orientation ="vertical" >
< ListView
android:id ="@+id/lv"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
/>
</ LinearLayout >
vlist.xml 这个是自定义 listview 的布局文件
<?
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 ="fill_parent" >
< ImageView
android:id ="@+id/img"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_margin ="3px"
/>
< LinearLayout
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:orientation ="vertical"
>
< 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 = "12px"
/>
</ LinearLayout >
</ LinearLayout >
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent" >
< ImageView
android:id ="@+id/img"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_margin ="3px"
/>
< LinearLayout
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:orientation ="vertical"
>
< 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 = "12px"
/>
</ LinearLayout >
</ LinearLayout >
在刚做出来后,一直没有办法运行,最终发现 TextView 这里写错了,改正确后就可以运行了。