Android SimpleCursorAdapter只显示字段名称而不是SQLite DB中的字段值

时间:2016-09-19 15:28:55

标签: android sqlite listview simplecursoradapter

使用SimpleCursorAdaptor的My ListView只显示每个数据库查询行的字段名称而不是字段值。

我的Java代码:

package com.kaiserware.sailingrace;

import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.SimpleCursorAdapter;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class list_wind_table extends Activity {
   private static final String LOG_TAG = list_wind_table.class.getSimpleName();
   private Cursor cursor = null;
   private SQLiteDatabase db = null;
   private sqlWindDataHelper dbhelper = new sqlWindDataHelper(this);

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      long raceID;

      super.onCreate(savedInstanceState);
      setContentView(R.layout.show_wind_data);
      setTitle("RaceApp - Wind Data Display");

      // check if we have any wind records for this raceID.
      db = dbhelper.getWritableDatabase();

      if (db.isOpen()) {
        // get the last race in the SQLite DB
        String sql = "SELECT * FROM "+sqlWindDataHelper.RaceEntry.TABLE_NAME;
        sql += " ORDER BY " + sqlWindDataHelper.RaceEntry.COLUMN_ID+" DESC";
        cursor = db.rawQuery(sql, null);
        Log.d(LOG_TAG, cursor.getCount()+" records found in query="+sql);

        if (cursor.moveToFirst()) {
            raceID = (long) cursor.getLong(sqlWindDataHelper.COL_RACE_ID);
            sql = "SELECT * FROM " + sqlWindDataHelper.WindEntry.TABLE_NAME;
            sql += " WHERE " + sqlWindDataHelper.WindEntry.COLUMN_RACE + "='" + raceID + "'";
            sql += " ORDER BY " + sqlWindDataHelper.WindEntry.COLUMN_DATE + " DESC";
        } else {
            raceID = 9999;
            sql = "SELECT * FROM " + sqlWindDataHelper.WindEntry.TABLE_NAME;
            sql += " ORDER BY " + sqlWindDataHelper.WindEntry.COLUMN_DATE + " DESC";
        }

        // fetch all wind records with the ID = raceID
        cursor = db.rawQuery(sql, null);
        if (cursor!=null && cursor.getCount()>0) {
            cursor.moveToFirst();
            Log.d(LOG_TAG, cursor.getCount()+" records found in query="+sql);

            String[] fromColumns = new String[] {
                sqlWindDataHelper.WindEntry.COLUMN_DATE,
                sqlWindDataHelper.WindEntry.COLUMN_AWD,
                sqlWindDataHelper.WindEntry.COLUMN_AWS,
                sqlWindDataHelper.WindEntry.COLUMN_TWD,
                sqlWindDataHelper.WindEntry.COLUMN_TWS
            };
            int[] toViews = new int[] {R.id.LV_date, R.id.LV_awd, R.id.LV_aws, R.id.LV_twd, R.id.LV_tws};

            // create the adapter using the cursor pointing to the desired data
            // as well as the row layout information
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                    R.layout.show_wind_data, cursor, fromColumns, toViews, 1);

            ListView WindListView = (ListView) findViewById(R.id.WindTableListView);
            if (WindListView == null) {
                Toast toast = Toast.makeText(this, "Could not initialize the Simple Cursor Adapter", Toast.LENGTH_LONG);
                toast.show();
            } else {
                WindListView.setAdapter(adapter);
            }
        } else {
            Toast toast = Toast.makeText(this, "No wind records found for Race ID "+raceID, Toast.LENGTH_LONG);
            toast.show();
        }
     } else {
        Toast toast = Toast.makeText(this, "Could not open the SQLite database", Toast.LENGTH_LONG);
        toast.show();
     }
   }

   @Override
   protected void onDestroy() {
    super.onDestroy();
    dbhelper.closeDB(cursor, db);
   }
}

这是我的两个xmls 1.)show_wind_data.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:layout_gravity="center_horizontal"
       >
       <TextView
           style="@style/TextAppearance.AppCompat.Headline"
           android:textColor="@color/WHITE"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="wrap_content"
           android:text="Date"
           />
       <TextView
           style="@style/TextAppearance.AppCompat.Headline"
           android:textColor="@color/WHITE"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="wrap_content"
           android:text="AWD"
           />
       <TextView
           style="@style/TextAppearance.AppCompat.Headline"
           android:textColor="@color/WHITE"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="wrap_content"
           android:text="AWS"
           />
       <TextView
           style="@style/TextAppearance.AppCompat.Headline"
           android:textColor="@color/WHITE"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="wrap_content"
           android:text="TWD"
           />
       <TextView
           style="@style/TextAppearance.AppCompat.Headline"
           android:textColor="@color/WHITE"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="wrap_content"
           android:text="TWS"
           />
   </LinearLayout>

   <ListView
       android:id="@+id/WindTableListView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
    </ListView>
</LinearLayout>

和2.)wind_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <TextView
        android:id="@+id/LV_date"
        android:textColor="@color/WHITE"
        style="@style/TextAppearance.AppCompat.Body2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/LV_awd"
        android:textColor="@color/WHITE"
        style="@style/TextAppearance.AppCompat.Body2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/LV_aws"
        android:textColor="@color/WHITE"
        style="@style/TextAppearance.AppCompat.Body2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/LV_twd"
        android:textColor="@color/WHITE"
        style="@style/TextAppearance.AppCompat.Body2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/LV_tws"
        android:textColor="@color/WHITE"
        style="@style/TextAppearance.AppCompat.Body2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
      />
</LinearLayout>

从调试代码我知道查询产生了26行风数据。但是LISTVIEW只生成26行: 约会AWD AWS TWD TWS 约会AWD AWS TWD TWS .....等等

我错过了什么?

PS:字符串变量sqlWindDataHelper.WindEntry.COLUMN_DATE =“Date”等包含SQLite DB Table列名。

1 个答案:

答案 0 :(得分:0)

masp,感谢您查看我的代码。伟大的捕获在你身边。不知道我几个小时都错过了。简单的修复,改变

            // create the adapter using the cursor pointing to the desired data
            // as well as the row layout information
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.show_wind_data, cursor, fromColumns, toViews, 1);

为:

            // create the adapter using the cursor pointing to the desired data
            // as well as the row layout information stored in wind_row.xml
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.wind_row, cursor, fromColumns, toViews, 1);

为了使用正确的xml和行列定义。

非常感谢!