Trying to develop a list view in Android with data in database
Hi i am trying to populate a list view with data in the database but i am not able to make it out.Here is the code i tried out
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);
}
});
}
}
My Database Class is
DBAdapter Class
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)
{
开发者_如何学C 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;
}
}
The XML used are vehicledisplay.xml and 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>
Please kindly help me on this
The problem that stands out to me here is that your table does not have its primary key column named _id
. CursorAdapter
uses this column internally to maintain consistency.
You should change the name of P_VEHID
to _id
(I recommend also adding NOT NULL
to its specification) or add an _id
column and add an index on P_VEHID
. Then change your select statement to read:
SELECT _id, P_VEHMD FROM vehicle WHERE P_EMPID = ?
精彩评论