0
基本上我有在DatabaseHelper擴展類中初始化數據庫的代碼(請參見下文),而我目前只獲取要顯示的一個結果?Android的SQLite數據使用SimpleCursorAdapter沒有在列表視圖中填充多個行?
@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>
謝謝,不敢相信我做了這樣一個愚蠢的業餘犯 –