2016-09-19 107 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); 

来。

非常感谢!

相关问题