2016-09-19 19 views
0

使用SimpleCursorAdaptor的我的ListView僅顯示每個DB查詢行的字段名稱,而不是字段值。Android SimpleCursorAdapter僅顯示字段名稱而不是SQLite數據庫中的字段值

我的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); 
    } 
} 

這裏是我的兩個個XML 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行風數據。然而列表視圖僅產生26行: DATE AWD AWS TWD TWS DATE AWD AWS TWD TWS .....等等

我缺少什麼?

PS:字符串變量sqlWindDataHelper.WindEntry.COLUMN_DATE =「Date」等包含SQLite數據庫表的列名。

+0

你在哪裏使用第二個xml,wind_row?您的活動中的代碼似乎使用simplecursoradapter中的第一個xml show_wind_data.xlm,但textview Ids來自第二個xml。 – masp

回答

0

masp,感謝您對我的代碼的評論。大受歡迎在你身邊。不知道我怎麼錯過了幾個小時。爲了使用正確的XML與該行列定義

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

:簡單的修復,改變

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

來。

非常感謝!

相關問題