0

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); 

どんな助けでも大歓迎です。

4

2 に答える 2

1

@+id/plate、@+id/hyphen、@+id/model、および @+id/driver でTextViews定義した には、属性と属性がすべてありません。これらは必須です。vehical_row.xmllayout_widthlayout_height

ここでは、これらの属性を「wrap_content」として設定します。例えば:

<TextView
        android:id="@+id/plate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/vehicleDefaultFont.plate"
        android:text="@string/label_text_view" />

他の 3 つについても上記の手順を実行します。これはうまくいくはずです。

于 2013-07-21T02:29:57.443 に答える