这里我是Android编程的新手, 我想创建一个在listview中显示数据库的活动但是当我运行那个活动时我得到了一些错误:
Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
我在google和Stackoverflow上搜索,我找到了一些答案
Cursor必须包含一个名为_id的列,否则该类不会 工作。您可以尝试使用现有ID伪造它
我试过这种方式,但现在我还有一些错误说
Caused by: android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT CategName, _id FROM category_table
我不知道这个错误究竟发生了什么,但我希望有人可以帮助我.. 请用简单易懂的词语掌握一些解释。 谢谢......
NB。这是我的活动,我得到了那些错误
public class CategorySetting extends ListActivity {
private SQLiteDatabase db;
private CursorAdapter currAdapter;
private static Button BtnIAddCateg;
private static Button BtnICancelCateg;
private static final String TAG = CategorySetting.class.getSimpleName();
DatabaseHelper dBHelper = new DatabaseHelper (this);
private ListView list;
private String tableName = DatabaseHelper.TABLE_Categ_NAME;
private String columnName = DatabaseHelper.COL2;
private CursorAdapter data_sumber;
private static final String kolom[] = {"CategName"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_category_setting);
//onButtonClickButtonListener();
ListView list = getListView();
// list = (ListView)findViewById(R.id.list);
ListView view = getListView();
view.addHeaderView(getLayoutInflater().inflate(R.layout.row, null));
db = dBHelper.getWritableDatabase();
this.muat_ulang();
}
public void muat_ulang()
{
Cursor data = db.query( tableName, kolom, null, null, null, null,null);
data_sumber = new SimpleCursorAdapter(this, R.layout.row, data, kolom,new int[] { R.id.list });
setListAdapter(data_sumber);
}
/*public void reload(){
try {
DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
db = dbHelper.getWritableDatabase();
Cursor c = db.rawQuery("SELECT CategName FROM " + tableName, null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String categName = c.getString(c.getColumnIndex("CategName"));
}while (c.moveToNext());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (db != null)
db.execSQL("DELETE FROM " + tableName);
db.close();
}
}*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_category_setting, menu);
return true;
}
public void onButtonClickButtonListener(){
BtnIAddCateg = (Button)findViewById(R.id.btnAddNewCateg);
BtnIAddCateg.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentAddCateg = new Intent ("com.example.ever_ncn.cashflow.AddCategory");
startActivity(intentAddCateg);
}
}
);
BtnICancelCateg = (Button)findViewById(R.id.btnCancelCateg);
BtnICancelCateg.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
}
);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}