我需要在 Android 中使用 simplecursor 适配器绑定 gridview,这是我的代码:
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);
}
}
但它给出了一个错误“索引 -1 请求大小为 18(16-20)”我做错了什么?
PS:我按照巴拉克的说法更改了它,但现在它给出了错误:“列 id 不存在”确实谢谢