I'm trying to display everything that was written to my app at runtime, but it displays the error below:
Unable to start activity ComponentInfo {com.project ....}: java.lang.IllegalArgumentException: column '_id' does not exist
BooksActivity:
public class LivrosActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_livros);
ListView lista;
BancoControlador control = new BancoControlador(getBaseContext());
Cursor cursor = control.carregaDados();
String[] nomeCampos = new String[]{CriaBanco.id, CriaBanco.livro};
int[] idViews = new int[] {R.id.idLivro, R.id.nomeLivro};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.activity_main, cursor, nomeCampos, idViews, 0);
lista = (ListView) findViewById(R.id.listViewLivros);
lista.setAdapter(adapter);
}}
Method that assign values:
public Cursor carregaDados(){
Cursor cursor;
String[] campos = {banco.id,banco.livro};
db = banco.getReadableDatabase();
cursor = db.query(banco.tabela, campos, null, null, null, null, null);
if(cursor!=null){
cursor.moveToFirst();
}
db.close();
return cursor;
}
XML:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listViewLivros"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2" >
<TextView
android:id="@+id/idLivro"
android:layout_width="0dip"
android:layout_gravity="fill_horizontal"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="ID" />
<TextView
android:id="@+id/nomeLivro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="Livro" />
</GridLayout>
OnCreate Method - OnUpgrade:
public CriaBanco(Context context) {
super(context, nomebanco, null, versao, null);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE "+tabela+"( "
+id+" integer primary key autoincrement, "
+livro+" text(40)"
+")";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+tabela);
onCreate(db);
}
}