我写的是存储在数据库中的列表视图通过cursor.but在运行一个程序,可以显示数据它给了我力量close.I读logcat的,但我不明白,这是什么error.I给我的代码和logcat.please帮助我
current_cart代码:
public class current_cart extends ListActivity {
private ListView mainListView = null;
CustomSqlCursorAdapter adapter = null;
private SqlHelper dbHelper = null;
private Cursor currentCursor = null;
private ListView listView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.current_cart);
if (this.dbHelper == null) {
this.dbHelper = new SqlHelper(this);
}
listView = getListView();
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//listView.setClickable(true);
Button btnClear = (Button) findViewById(R.id.btnClear);
btnClear.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
" You clicked Clear button", Toast.LENGTH_SHORT).show();
// ClearDBSelections();
}
});
new SelectDataTask().execute();
this.mainListView = getListView();
mainListView.setCacheColorHint(0);
}
@Override
protected void onRestart() {
super.onRestart();
new SelectDataTask().execute();
}
@Override
protected void onPause() {
super.onPause();
this.dbHelper.close();
}
// protected void ClearDBSelections() {
// this.adapter.ClearSelections();
// }
private class SelectDataTask extends AsyncTask<Void, Void, String> {
protected String doInBackground(Void... params) {
try {
current_cart.this.dbHelper.createDatabase(dbHelper.dbSqlite);
current_cart.this.dbHelper.openDataBase();
current_cart.this.currentCursor = current_cart.this.dbHelper
.getCursor();
}
catch (SQLException sqle)
{
throw sqle;
}
return null;
}
// can use UI thread here
protected void onPostExecute(final String result) {
startManagingCursor(current_cart.this.currentCursor);
int[] listFields = new int[] { R.id.txtTitle,R.id.txtprice };
String[] dbColumns = new String[] { SqlHelper.COLUMN_TITLE,SqlHelper.COLUMN_NAME_DESC };
current_cart.this.adapter = new CustomSqlCursorAdapter(
current_cart.this, R.layout.single_item,
current_cart.this.currentCursor, dbColumns, listFields,
current_cart.this.dbHelper);
setListAdapter(current_cart.this.adapter);
}
}
}
CustomSqlCursorAdapter代码:
public class CustomSqlCursorAdapter extends SimpleCursorAdapter {
private Context mContext;
private SqlHelper mDbHelper;
private Cursor mCurrentCursor;
public CustomSqlCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, SqlHelper dbHelper) {
super(context, layout, c, from, to);
this.mCurrentCursor = c;
this.mContext = context;
this.mDbHelper = dbHelper;
}
public View getView(int pos, View inView, ViewGroup parent) {
View v = inView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.single_item, null);
}
if (!this.mCurrentCursor.moveToPosition(pos)) {
throw new SQLException("CustomSqlCursorAdapter.getView: Unable to move to position: "+pos);
}
CheckBox cBox = (CheckBox) v.findViewById(R.id.bcheck);
cBox.setTag(Integer.valueOf(this.mCurrentCursor.getInt(0)));
cBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
TextView txtTitle = (TextView) v.findViewById(R.id.txtTitle);
txtTitle.setText(this.mCurrentCursor.getString(this.mCurrentCursor
.getColumnIndex(SqlHelper.COLUMN_TITLE)));
TextView txtprice = (TextView) v.findViewById(R.id.txtprice);
txtprice.setText(this.mCurrentCursor.getString(this.mCurrentCursor
.getColumnIndex(SqlHelper.COLUMN_NAME_DESC)));
return (v);
}
}
提供SQLHelper代码:
public class SqlHelper extends SQLiteOpenHelper {
private static final String DATABASE_PATH = "/data/data/com.example.bb/databases/";
public static final String DATABASE_NAME = "ll";
public static final String TABLE_NAME = "w";
public static final int ToDoItems_VERSION = 1;
// public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "Good_Name";
public static final String COLUMN_NAME_DESC = "Good_UnitPrice";
public SQLiteDatabase dbSqlite;
private Context mContext;
public SqlHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
createDB(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("SqlHelper", "Upgrading database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS w;");
createDB(db);
}
public void createDatabase(SQLiteDatabase db) {
createDB(db);
}
private void createDB(SQLiteDatabase db) {
if (db == null) {
db = mContext.openOrCreateDatabase(DATABASE_NAME, 0, null);
}
db.execSQL("CREATE TABLE IF NOT EXISTS w (Cart_ID INTEGER, Good_Name VARCHAR(50),Good_UnitPrice INTEGER (10),Quantity INTEGER);");
db.setVersion(ToDoItems_VERSION);
}
public void openDataBase() throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if (dbSqlite != null)
dbSqlite.close();
super.close();
}
public Cursor getCursor() {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(TABLE_NAME);
String[] asColumnsToReturn = new String[] {COLUMN_TITLE,COLUMN_NAME_DESC};
Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
null, null, null, null);
return mCursor;
}
// public void clearSelections() {
// ContentValues values = new ContentValues();
// values.put(COLUMN_SELECTED, 0);
// this.dbSqlite.update(SqlHelper.TABLE_NAME, values, null, null);
// }
}
我logcat的:
12-19 14:36:02.999: E/AndroidRuntime(436): FATAL EXCEPTION: main
12-19 14:36:02.999: E/AndroidRuntime(436): java.lang.IllegalArgumentException: column '_id' does not exist
12-19 14:36:02.999: E/AndroidRuntime(436): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:297)
12-19 14:36:02.999: E/AndroidRuntime(436): at android.widget.CursorAdapter.init(CursorAdapter.java:169)
12-19 14:36:02.999: E/AndroidRuntime(436): at android.widget.CursorAdapter.<init>(CursorAdapter.java:117)
12-19 14:36:02.999: E/AndroidRuntime(436): at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:52)
12-19 14:36:02.999: E/AndroidRuntime(436): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:78)
12-19 14:36:02.999: E/AndroidRuntime(436): at com.example.nfc.CustomSqlCursorAdapter.<init>(CustomSqlCursorAdapter.java:28)
12-19 14:36:02.999: E/AndroidRuntime(436): at com.example.nfc.current_cart$SelectDataTask.onPostExecute(current_cart.java:110)
12-19 14:36:02.999: E/AndroidRuntime(436): at com.example.nfc.current_cart$SelectDataTask.onPostExecute(current_cart.java:1)
12-19 14:36:02.999: E/AndroidRuntime(436): at android.os.AsyncTask.finish(AsyncTask.java:590)
12-19 14:36:02.999: E/AndroidRuntime(436): at android.os.AsyncTask.access$600(AsyncTask.java:149)
12-19 14:36:02.999: E/AndroidRuntime(436): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:603)
12-19 14:36:02.999: E/AndroidRuntime(436): at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 14:36:02.999: E/AndroidRuntime(436): at android.os.Looper.loop(Looper.java:126)
12-19 14:36:02.999: E/AndroidRuntime(436): at android.app.ActivityThread.main(ActivityThread.java:3997)
12-19 14:36:02.999: E/AndroidRuntime(436): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 14:36:02.999: E/AndroidRuntime(436): at java.lang.reflect.Method.invoke(Method.java:491)
12-19 14:36:02.999: E/AndroidRuntime(436): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-19 14:36:02.999: E/AndroidRuntime(436): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-19 14:36:02.999: E/AndroidRuntime(436): at dalvik.system.NativeStart.main(Native Method)