2011-09-06 76 views
0

你好我试图填充数据库中的数据的列表视图,但我不能让它out.Here是我尝试了试图开发在Android中的列表视图与数据库中的数据

代码VehicleDisplay类

​​

我的数据库类是

将对DBAdapter类

package com.android.allianz; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class DBAdapter 
{ 

    public static final String V_EMPID = "P_EMPID"; 
    public static final String V_VEHID = "P_VEHID"; 
    public static final String V_VEHTP = "P_VEHTP"; 
    public static final String V_VEHMD = "P_VEHMD"; 
    public static final String V_REGYR = "P_REGYR"; 
    public static final String V_REGNO = "P_REGNO"; 
    public static final String V_INSNM = "P_INSNM"; 
    public static final String V_INSNO = "P_INSNO"; 
    public static final String V_INSVY = "P_INSVY"; 


    private static final String TAG = "DBAdapter"; 

    private static final String DATABASE_NAME = "usersdb"; 
    private static final String DATABASE_TABLE4 = "vehicle"; 
    private static final int DATABASE_VERSION = 1; 



    private static final String DATABASE_CREATE4 = 
     "create table vehicle (P_VEHID integer primary key, " 
     + "P_EMPID text, " 
     + "P_VEHTP text, " 
     + "P_VEHMD text, " 
     + "P_REGYR text, " 
     + "P_REGNO text, " 
     + "P_INSNM text, " 
     + "P_INSNO text, " 
     + "P_INSVY text);"; 

    private Context context = null; 
    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 

    public DBAdapter(Context ctx) 
    { 
     this.context = ctx; 
     DBHelper = new DatabaseHelper(context); 
    } 

    public static class DatabaseHelper extends SQLiteOpenHelper 
    { 
     DatabaseHelper(Context context) 
     { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) 
     { 

      db.execSQL(DATABASE_CREATE4); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
     { 
      Log.w(TAG, "Upgrading database from version " + oldVersion 
        + " to " 
        + newVersion + ", which will destroy all old data"); 

      db.execSQL("DROP TABLE IF EXISTS vehicle"); 
      onCreate(db); 
     } 
    }  


    public void open() throws SQLException 
    { 
     db = DBHelper.getWritableDatabase(); 
    } 


    public void close() 
    { 
     DBHelper.close(); 
    }  


    public Boolean AddVehicle(String P_EMPID,String P_VEHTP, String P_VEHMD, 
      String P_REGYR, String P_REGNO, String P_INSNM,String P_INSNO, String P_INSVY) 
{ 
Boolean bool = false; 
ContentValues initialValues = new ContentValues(); 
initialValues.put(V_EMPID, P_EMPID); 
initialValues.put(V_VEHTP, P_VEHTP); 
initialValues.put(V_VEHMD, P_VEHMD); 
initialValues.put(V_REGYR, P_REGYR); 
initialValues.put(V_REGNO, P_REGNO); 
initialValues.put(V_INSNM, P_INSNM); 
initialValues.put(V_INSNO, P_INSNO); 
initialValues.put(V_INSVY, P_INSVY); 

if(db.insert(DATABASE_TABLE4, null, initialValues)> 0) 
{ 
bool = true; 
} 
else {bool = false;} 

return bool; 
} 


    public boolean Vehicle(String vehtype,String vehModel,String purYear,String regisNo,String insurName,String insurNo,String insurVdty) throws SQLException 
    { 
     Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE4 + " WHERE P_VEHTP=? AND P_VEHMD=? AND P_REGYR=? AND P_REGNO=? AND P_INSNM=? AND P_INSNO=? AND P_INSVY=?", new String[]{vehtype,vehModel,purYear,regisNo,insurName,insurNo,insurVdty}); 
     if (mCursor != null) {   
      if(mCursor.getCount() > 0) 
      { 
       return true; 
      } 
     } 
    return false; 
    } 


} 

中使用的XML是vehicledisplay.xml和vehiclerow.xml

vehicledisplay XML

<?xml version="1.0" encoding="utf-8"?> 


<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/vehicledisp" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
     <ListView android:layout_width="match_parent" 
        android:id="@+id/vehcleList" android:layout_height="377dp"> 

     </ListView> 
     <Button android:id="@+id/add_vehicle" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@string/addvehicle"></Button> 
    </LinearLayout> 
</ScrollView> 

vehiclerow XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView android:id="@+id/vehMdl" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
     </TextView> 
     </RelativeLayout> 

敬请帮我在这

+0

您确定您正确使用列表适配器和数据库吗?您是否尝试使用硬编码值填充列表?您是否尝试使用LogCat列出数据库内容? – slayton

回答

1

这里突出的问题是您的表没有名为_id的主键列。 CursorAdapter在内部使用此列来保持一致性。

你应该改变的P_VEHID_id名称(我还建议增加NOT NULL其规格)或添加_id列和P_VEHID添加索引。然后将您的选择语句更改为:

SELECT _id,P_VEHMD FROM vehicle WHERE P_EMPID =?

+0

适配器不能在活动中工作你能帮我解决这个问题 – sunit

+0

这似乎是因为你已经将适配器设置到列表视图的那一行注释掉了。 – Greyson

相关问题