SimpleCursorAdapter 没有填充我的 ListView,没有错误,只是没有任何反应。
有人可以帮我找出错误吗?
1) 使用 ListView 组件布局 (tab_frag1_layout.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:background="#FF0000"
android:orientation="vertical" >
<ListView
android:id="@+id/lvVehicle"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
2) 表示行的布局 (vehicle_row.xml):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dip" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_arrow_right"
android:contentDescription="@null"
android:adjustViewBounds="true"
android:layout_marginRight="3dip"
android:layout_gravity="center_vertical" />
<TextView
android:id="@+id/plate"
style="@style/vehicleDefaultFont.plate"
android:text="@string/label_text_view" />
<TextView
android:id="@+id/hyphen"
android:text="@string/label_hyphen" />
<TextView
android:id="@+id/model"
android:text="@string/label_text_view" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dip" >
<TextView
android:id="@+id/driver"
style="@style/vehicleDefaultFont.driver"
android:layout_span="4"
android:text="@string/label_text_view" />
</TableRow>
</TableLayout>
3)片段类:
public class Tab1Fragment extends Fragment {
String TAG = getClass().getName();
private VehicleDbAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_frag1_layout, container, false);
dbHelper = new VehicleDbAdapter(getActivity());
dbHelper.open();
dbHelper.deleteAllVehicles();
dbHelper.insertVehicles();
Cursor cursor = dbHelper.fetchAllVehicles();
if(cursor != null && cursor.getCount() > 0) {
String[] columns = new String[] {
VehicleDbAdapter.KEY_MODEL,
VehicleDbAdapter.KEY_PLATE,
VehicleDbAdapter.KEY_DRIVER
};
int[] to = new int[] {
R.id.model,
R.id.plate,
R.id.driver,
};
dataAdapter = new SimpleCursorAdapter(
view.getContext(),
R.layout.vehicle_row,
cursor,
columns,
to,
0);
ListView listView = (ListView) view.findViewById(R.id.lvVehicle);
listView.setAdapter(dataAdapter);
Log.i(TAG, "Cursor = " + cursor.getCount());
} else {
Log.i(TAG, "Cursor = " + cursor.getCount());
}
return view;
}
}
奇怪的是:
我的光标包含一个 _id 字段。
我仔细检查了我的光标,它有 7 行。
07-21 01:30:22.215: I/光标 = 7
我尝试将布局用于通用 android 列表布局,但什么也没做:(
dataAdapter = new SimpleCursorAdapter(
view.getContext(),
android.R.layout.simple_list_item_1,
cursor,
columns,
to,
0);
欢迎任何帮助。