重要的几个文件SQLiteHelper.java
package com.eclipghc.sqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SQLiteHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "brushdrill.db";
private static final String TBL_NAME = "tbl_bdrill";
private static final String CONTENT = "content", NUNMBER = "number", _ID = "_ID";
public SQLiteHelper(Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + TBL_NAME + " (" + _ID
+ " integer primary key autoincrement, " + CONTENT
+ " text, " + NUNMBER + " text);";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS " + TBL_NAME;
db.execSQL(sql);
onCreate(db);
}
// 添加方法
public void insert(String content, String number) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(CONTENT, content);
values.put(NUNMBER, number);
db.insert(TBL_NAME, null, values);
db.close();
}
// 查询方法
public Cursor query() {
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.query(TBL_NAME, null, null, null, null, null, null);
return cursor;
}
// 删除方法
public void delete(int id) {
SQLiteDatabase db = getWritableDatabase();
String where = _ID + " = ? ";
String[] whereValue = { Integer.toString(id) };
db.delete(TBL_NAME, where, whereValue);
}
// 修改方法
public void update(int id, String content, String number) {
SQLiteDatabase db = getWritableDatabase();
String where = _ID + " = ? ";
String[] whereValue = { Integer.toString(id)};
ContentValues values = new ContentValues();
values.put(CONTENT, content);
values.put(NUNMBER, number);
db.update(TBL_NAME, values, where, whereValue);
}
} MainActivity.java
package com.eclipghc.brushdrill;
import com.eclipghc.sqlite.SQLiteHelper;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends Activity {
private Button addButton, outButton;
private ListView listView;
private EditText editText1, editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addButton = (Button) findViewById(R.id.add_button);
addButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final SQLiteHelper helper = new SQLiteHelper(MainActivity.this);
View view = getLayoutInflater().inflate(R.layout.alertdialog_main_context, null);
editText1 = (EditText) view.findViewById(R.id.editText1);
editText2 = (EditText) view.findViewById(R.id.editText2);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.add_button);
builder.setIcon(android.R.drawable.ic_menu_add);
builder.setView(view);
builder.setPositiveButton(R.string.yes, new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String content = editText1.getText().toString();
String number = editText2.getText().toString();
if (content.equals("") || number.equals("")) {
return;
}
helper.insert(content, number);
}
});
builder.setNegativeButton(R.string.no, null);
builder.show();
}
});
outButton = (Button) findViewById(R.id.out_button);
outButton.setOnClickListener(null);
listView = (ListView) findViewById(android.R.id.list);
SQLiteHelper helper = new SQLiteHelper(MainActivity.this);
Cursor c = helper.query();
String[] from = {"content","number"};
int[] to = {R.id.textViewContent,R.id.textViewNumber};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.list_main, c, from, to);
listView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
} logCat 总是报错 SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.list_main, c, from, to); 这句不知道那里出错,帮帮忙啊?还在学习,不要见笑?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
怎么没人回答呢?帮帮我,该怎么做,那里错了啊你应该把错误信息贴出来你看看log,里面有个_id列不存在的错误,意思是你这个table里面没有这一列啊,仔细检查下代码。因为SimpleCursorAdapter继承ResourceCursorAdapter,ResourceCursorAdapter继承CursorAdapter,CursorAdapter的构造函数中有这么一段c.getColumnIndexOrThrow("_id")代码,如果table中没有名为_id的列,则会抛出异常。你的问题就在此,在你的table中加上名为_id的列就OK了"_ID"这个应该换成小写