Android で gridview を simplecursor アダプターにバインドする必要があります。コードは次のとおりです。
public class AndroidSqliteActivity extends Activity {
private Veritabani ogrenciler;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
ogrenciler = new Veritabani(this);
final EditText adi=(EditText) findViewById(R.id.editText1);
final EditText soyadi=(EditText) findViewById(R.id.editText2);
Button verigonder=(Button) findViewById(R.id.verigonder);
Button button1 =(Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try{
Intent i = new Intent(AndroidSqliteActivity.this, AndroidVeriActivity.class);
startActivity(i);
}
catch(RuntimeException e)
{
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
}
}
});
verigonder.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try{
KayitEkle(adi.getText().toString(),soyadi.getText().toString());
Cursor cursor = KayitGetir();
KayitGoster(cursor);
}
catch (SQLiteException e) {
Log.d("eHata", e.getLocalizedMessage());
}
finally{
ogrenciler.close();
}
}
});
}
private void KayitEkle(String isim, String soyad){
SQLiteDatabase db = ogrenciler.getWritableDatabase();
ContentValues veriler = new ContentValues();
veriler.put("isim", isim);
veriler.put("soyad",soyad);
db.insertOrThrow("ogrenciisim", null, veriler);
}
private Cursor KayitGetir(){
SQLiteDatabase db = ogrenciler.getReadableDatabase();
Cursor cursor = db.query("ogrenciisim", new String[] {"id", "isim", "soyad"}, null, null, null, null, null);
startManagingCursor(cursor);
return cursor;
}
private void KayitGoster(Cursor cursor){
try{
final GridView grid=(GridView) findViewById(R.id.gridView1);
String[] columns = new String[] { "id", "isim", "soyad" };
int[] to = new int[] { R.id.textView122, R.id.textView222, R.id.textView322 };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.gridview, cursor, columns, to);
grid.setAdapter(mAdapter);
}
catch(Exception e)
{
Log.d("eHata", e.getLocalizedMessage());
}
}
}
そして、ここに私のデータベースクラスがあります:
public class Veritabani extends SQLiteOpenHelper {
private static final String VERITABANI = "Ogrenciler";
private static final int SURUM = 1;
public Veritabani(Context con)
{
super (con,VERITABANI,null,SURUM);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table ogrenciisim(id integer primary key, isim text, soyad text);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exist ogrenciisim");
onCreate(db);
}
}
しかし、「index -1 requested with size of 18(16-20)」というエラーが表示されます。
PS:バラクが述べたように変更しましたが、今ではエラーが発生します:「列IDが存在しません」本当にありがとう