SimpleCursorAdapter沒有填充我的ListView,沒有錯誤,只是沒有任何反應。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行。
一月7日至21日:30:22.215:I /光標= 7
我嘗試使用佈局通用的Android列表佈局,但沒有做:(
dataAdapter = new SimpleCursorAdapter(
view.getContext(),
android.R.layout.simple_list_item_1,
cursor,
columns,
to,
0);
任何幫助是值得歡迎。
你是那個人,哈哈。非常感謝你布魯! – vinidog
@vinidog沒問題,男人 – Vikram