//ListActivity绑定动态数据,本例使用SimpleCursorAdapter //bookmark3.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="8dp" android:paddingRight="8dp"> <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00FF00" android:layout_weight="1" android:drawSelectorOnTop="false"/> <TextView android:id="@+id/android:empty" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF0000" android:text="No data"/> </LinearLayout> //BookMarks3.java: package com.cx_browser; import com.cx_browser.db.DatabaseHelper; import android.app.Activity; import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class BookMarks3 extends ListActivity{ @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); Intent intent=new Intent(); //String et1=e1.getText().toString(); String et1="www.baidu.com";//换成数据库中取来的地址 intent.putExtra("URL_Wlc", et1); intent.setClass(BookMarks3.this,WebPage.class); BookMarks3.this.startActivity(intent); } //数据库名 private static final String DATABASE_NAME="test_db4"; //表名 private static final String TABLE_NAME="bookmarks"; //表中字段 private static final String TABLE_ID="_id"; private static final String TABLE_TITLE="title"; private static final String TABLE_URL="url"; //创建ListView列表视图 ListView m_ListView =null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.bookmarks3); //m_ListView=(ListView)this.findViewById(R.id.list1); //创建ListView对象 // m_ListView = new ListView(this); DatabaseHelper dbHelper=new DatabaseHelper(BookMarks3.this,DATABASE_NAME); //SQLiteDatabase db=dbHelper.getWritableDatabase(); SQLiteDatabase db=dbHelper.getReadableDatabase(); Cursor cursor=db.query(TABLE_NAME,new String[]{TABLE_ID,TABLE_TITLE,TABLE_URL},null,null,null, null,null); //String TITLE=cursor.getString(cursor.getColumnIndex("title")); //String URL=cursor.getString(cursor.getColumnIndex("url")); //startManagingCursor(cursor); // Now create a new list adapter bound to the cursor. // SimpleListAdapter is designed for binding to a Cursor. //获得查询的数据的条数 int count = cursor.getCount(); //if(cursor !=null && count>=0){ System.out.println("Count--->"+count); System.out.println(TABLE_TITLE); System.out.println(TABLE_URL); SimpleCursorAdapter adapter = new SimpleCursorAdapter( this, // Context. // android.R.layout.two_line_list_item, android.R.layout.simple_expandable_list_item_1, // Specify the row template to use (here, two columns bound to the two retrieved cursor rows). cursor, // Pass in the cursor to bind to. new String[] {TABLE_TITLE}, // Array of cursor columns to bind to. new int[] {android.R.id.text1}); // Parallel array of which template objects to bind to those columns. // Bind to our new adapter. setListAdapter(adapter); // } // cursor.close(); } } //另外可以使用maps方式静态的绑定数据: //a3.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="8dip" android:paddingRight="8dip" android:paddingBottom="8dip"> <TextView android:id="@+id/name" android:textSize="16sp" android:textStyle="bold" android:layout_width="180dip" android:layout_height="25dip"/> <TextView android:id="@+id/ip" android:textSize="16sp" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> //Activity3.java: package com.helloworld; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.app.ListActivity; import android.os.Bundle; import android.widget.SimpleAdapter; import android.widget.TextView; public class Activity3 extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //setContentView(R.layout.a3); //TextView t1=(TextView) this.findViewById(R.id.textView1); 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", "chenxi"); map1.put("ip", "192.168.1.0"); map2.put("name", "zhoujielun"); map2.put("ip", "192.168.1.1"); map3.put("name", "caiyilin"); map3.put("ip", "192.168.1.2"); list.add(map1); list.add(map2); list.add(map3); SimpleAdapter listAdapter=new SimpleAdapter(this, list, R.layout.a3,new String[]{"name","ip"}, new int[]{R.id.name,R.id.ip}); setListAdapter(listAdapter); } } //详情可见翻译SDK文章: //http://blog.csdn.net/flyfish10000/archive/2010/07/18/5745024.aspx