您好我正在尝试使用数据库中的数据填充列表视图但我无法将其删除。这是我尝试过的代码
VehicleDisplay Class
package com.android.allianz;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class VehicleDisplay extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vehicledisplay);
SQLiteDatabase db = (new DBAdapter.DatabaseHelper(this)).getWritableDatabase();
ListView vehList = (ListView)findViewById(R.id.vehcleList);
Cursor c = db.rawQuery("SELECT P_VEHMD FROM vehicle WHERE P_EMPID = ?",
new String[]{"i82112"});
ListAdapter adap = new SimpleCursorAdapter(
this,
R.layout.vehiclerow,
c,
new String[] {"P_VEHMD"},
new int[] {R.id.vehMdl});
/* vehList.setAdapter(adap);*/
Button bankaccount = (Button) findViewById(R.id.add_vehicle);
bankaccount.setOnClickListener(new View.OnClickListener(){
public void onClick(View z){
Intent bank = new Intent(VehicleDisplay.this,VehicleActivity.class);
startActivity(bank);
}
});
}
}
我的数据库类是
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 :(得分:1)
这里突出的问题是您的表没有名为_id
的主键列。 CursorAdapter
在内部使用此列来保持一致性。
您应该将P_VEHID
的名称更改为_id
(我建议在其规范中添加NOT NULL
)或添加_id
列并在{{P_VEHID
上添加索引1}}。然后将select语句更改为:
SELECT _id,P_VEHMD FROM车辆WHERE P_EMPID =?