最近我在android中使用了sqlite而且我的问题没有解决。
我找到了一个教程,解释了如何运行数据库并完美运行。我很高兴找到了。 现在我想实现一个CursorAdapter来自定义包含DB数据的listview ...但我总是遇到同样的错误。 我读了很多关于它的事情,我遇到的人(在这个网站上)发生了同样的事情,但无法理解答案。我有几天这个问题。
首先显示代码,这是我的主要活动:
public class RegistrosActivity extends Activity {
private ListView listView;
private Adapter_bd_notificaciones customAdapter;
private SQLite sqlite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registros);
//
listView = (ListView) findViewById(R.id.lstRegistros);
//Open conexion sqlite
sqlite = new SQLite(this);
sqlite.abrir();
Cursor cursor = sqlite.getRegistros();
customAdapter = new Adapter_bd_notificaciones(RegistrosActivity.this, cursor);
listView.setAdapter(customAdapter);
}
// the rest of the code
}
SQLite.java
public class SQLite {
private SQLiteHelper sqliteHelper;
private SQLiteDatabase db;
public SQLite(Context context)
{
sqliteHelper = new SQLiteHelper( context );
}
public void abrir(){
Log.i("SQLite", "Se abre conexion a la base de datos " + sqliteHelper.getDatabaseName() );
db = sqliteHelper.getWritableDatabase();
}
public void cerrar()
{
Log.i("SQLite", "Se cierra conexion a la base de datos " + sqliteHelper.getDatabaseName() );
sqliteHelper.close();
}
public boolean addRegistro( String nombre, String fecha, String pais, String sexo, String ingles )
{
if( nombre.length()> 0 )
{
ContentValues contentValues = new ContentValues();
contentValues.put( sqliteHelper.__campo_nombre , nombre);
contentValues.put( sqliteHelper.__campo_fechanac , fecha );
contentValues.put( sqliteHelper.__campo_pais , pais);
contentValues.put( sqliteHelper.__campo_sexo , sexo);
contentValues.put( sqliteHelper.__campo_ingles , ingles);
Log.i("SQLite", "Nuevo registro " );
return ( db.insert( sqliteHelper.__tabla__ , null, contentValues ) != -1 )?true:false;
}
else
return false;
}
public int getUltimoID()
{
int id = -1;
Cursor cursor = db.query( sqliteHelper.__tabla__ ,
new String[]{ sqliteHelper.__campo_id },
null, null, null,null,
sqliteHelper.__campo_id + " DESC ", "1");
if( cursor.moveToFirst() )
{
do
{
id = cursor.getInt(0);
} while ( cursor.moveToNext() );
}
return id;
}
public boolean borrar_registro( int id )
{
return (db.delete( sqliteHelper.__tabla__ , sqliteHelper.__campo_id + " = " + id , null) > 0) ? true:false;
}
public Cursor getRegistros()
{
return db.query( sqliteHelper.__tabla__ ,
new String[]{
sqliteHelper.__campo_id ,
sqliteHelper.__campo_nombre,
sqliteHelper.__campo_fechanac,
sqliteHelper.__campo_pais,
sqliteHelper.__campo_sexo,
sqliteHelper.__campo_ingles
},
null, null, null, null, null);
}
public Cursor getRegistro( int id )
{
return db.query( sqliteHelper.__tabla__ ,
new String[]{
sqliteHelper.__campo_id ,
sqliteHelper.__campo_nombre,
sqliteHelper.__campo_fechanac,
sqliteHelper.__campo_pais,
sqliteHelper.__campo_sexo,
sqliteHelper.__campo_ingles
},
sqliteHelper.__campo_id + " = " + id ,
null, null, null, null);
}
public ArrayList<String> getFormatListUniv( Cursor cursor )
{
ArrayList<String> listData = new ArrayList<String>();
String item = "";
if( cursor.moveToFirst() )
{
do
{
item += "ID: [" + cursor.getInt(0) + "]\r\n";
item += "Nombre: " + cursor.getString(1) + "\r\n";
item += "Fecha de Nacimiento: " + cursor.getString(2) + "\r\n";
item += "Pais: " + cursor.getString(3) + "\r\n";
item += "Sexo: " + cursor.getString(4) + "\r\n";
item += "Habla ingles: " + cursor.getString(5) + "";
listData.add( item );
item="";
} while ( cursor.moveToNext() );
}
return listData;
}
}
SQLIteHelper.java
public class SQLiteHelper extends SQLiteOpenHelper {
private static final String __DATABASE = "dbUniversidad";
private static final int __VERSION = 3;
public final String __tabla__ = "Universitario";
public final String __campo_id = "id";
public final String __campo_nombre = "Nombre";
public final String __campo_fechanac = "FechaNac";
public final String __campo_pais = "Pais";
public final String __campo_sexo = "Sexo";
public final String __campo_ingles = "Ingles";
private final String sql = "CREATE TABLE " + __tabla__ + " ( " +
__campo_id + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
__campo_nombre + " TEXT NULL, " + __campo_fechanac + " TEXT, " + __campo_pais + " TEXT NULL, " +
__campo_sexo + " TEXT NULL, " + __campo_ingles + " TEXT NULL " +
" )";
public SQLiteHelper(Context context) {
super( context, __DATABASE, null, __VERSION );
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL( sql );
}
@Override
public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion ) {
if ( newVersion > oldVersion )
{
//elimina tabla
db.execSQL( "DROP TABLE IF EXISTS " + __tabla__ );
//y luego creamos la nueva tabla
db.execSQL( sql );
}
}
}
正如我所说,经过两天的许多变化和变化,阅读了很多,我总是发现同样的错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{my.database.example/my.database.example.RegistrosActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
我知道大多数人都是一样的,但即使阅读正确答案,我也不知道如何应用我的代码。
列'_id'不存在
请有人告诉我如何在我的代码中正确使用CursorAdapter吗?我错过了哪一行?我究竟做错了什么? 如果你能纠正我的代码,我将不胜感激。
非常感谢您的帮助。
问候!
PS。好像这很有用,这就是完整的错误:
FATAL EXCEPTION: main
Process: my.database.example, PID: 31765
java.lang.RuntimeException: Unable to start activity ComponentInfo{my.database.example/my.database.example.RegistrosActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
at android.widget.CursorAdapter.init(CursorAdapter.java:172)
at android.widget.CursorAdapter.<init>(CursorAdapter.java:120)
at my.database.example.Adapter_bd_notificaciones.<init>(Adapter_bd_notificaciones.java:15)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
答案 0 :(得分:1)
正如Blackbelt在评论中提到的,Android期望主标识符列被命名为“_id”。