基本上我有在 DatabaseHelper 扩展类中初始化数据库的代码(见下文),我目前只得到一个结果来显示?
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS host ("
+ BaseColumns._ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, mac_address VARCHAR)");
ContentValues host_values = new ContentValues();
host_values.put("name", "test host");
host_values.put("mac_address", "ffffffffffff");
db.insert("host", null, host_values);
ContentValues host_values2 = new ContentValues();
host_values.put("name", "test me host");
host_values.put("mac_address", "eeeeeeeeeeee");
db.insert("host", null, host_values2);
}
这是代码,我在 ListActivity 上使用 ListFragment 有什么区别吗?
public class TargetListFragment extends ListFragment {
private SQLiteDatabase database;
private CursorAdapter dataSource;
private static final String fields[] = { "name", "mac_address",
BaseColumns._ID };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
DatabaseHelper helper = new DatabaseHelper(this.getActivity().getApplicationContext());
database = helper.getWritableDatabase();
Cursor data = database.query("host", fields,
null, null, null, null, null);
dataSource = new SimpleCursorAdapter(this.getActivity().getApplicationContext(),
R.layout.target_row, data, fields,
new int[] { R.id.target_name, R.id.mac_address });
setListAdapter(dataSource);
}
这真的让我很困扰,因为光标似乎只处理第一行然后被关闭,但我想不出有什么会导致这种情况?
再次以防万一,这里是行视图的 XML。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:id="@+id/target_row_layout"
android:orientation="vertical" android:layout_width="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/target_name"
style="@style/target_list.item_name" /> <!--style="@style/target_list.item_name"-->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/mac_address"
style="@style/target_list.mac_address" /> <!--style="@style/target_list.mac_address"-->
</LinearLayout>