每次调试项目时,我的设备都会显示我的应用程序停止的对话框。这是我的代码。我不知道我的代码有什么问题。
public class Nasabah extends Activity {
DatabaseHelper db = new DatabaseHelper(this);
ArrayList<HashMap<String,String>> contactList;
private static final String TAG_NAME = "name";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nasabah);
final ListView listNasabah = (ListView) findViewById(R.id.listNasabah);
Log.d("Insert: ", "Inserting...");
db.addContact(new Contact("Jose Mourinho", "087888888888", "1088994444"));
db.addContact(new Contact("Didier Drogba", "0878555555555", "1088996666"));
db.addContact(new Contact("Eden Hazard", "0878511111111", "1088995555"));
Log.d("Reading: ", "Reading all contacts...");
List<Contact> contact = db.getAllContacts();
contactList = new ArrayList<HashMap<String, String>>();
for (Contact cn : contact){
String log = "Id :"+cn.getId()+" ,Name: "+cn.getName()+" ,Telp: "+cn.getTelp()+" ,Rek: "+cn.getRek();
Log.d("Name: ", log);
String name = "" + cn.getName();
HashMap <String, String> contacts = new HashMap<String, String>();
contacts.put(TAG_NAME, name);
contactList.add(contacts);
}
SimpleAdapter adapter = new SimpleAdapter(this, contactList, android.R.layout.simple_list_item_1, new String[]{TAG_NAME}, new int[]{android.R.id.text1});
listNasabah.setAdapter(adapter);
listNasabah.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
}
});
}}
这是我的 databasehelper.java :
public class DatabaseHelper extends SQLiteOpenHelper{
private static final String DB_NAME = "BMIcontact.db";
private static final int DB_VER = 1;
private static final String TABLE_NAME = "contact";
private static final String COL_ID = "id_nasabah";
private static final String COL_NAME = "nama_nasabah";
private static final String COL_TELP = "telp";
private static final String COL_REK = "rekening";
public DatabaseHelper (Context context){
super(context, DB_NAME, null, DB_VER);
//super(context, Environment.getExternalStorageDirectory() + File.separator + "/DataBase/" + File.separator + DB_NAME, null, DB_VER);
}
@Override
public void onCreate(SQLiteDatabase db){
String CREATE_CONTACT_TABLE = "CREATE TABLE" + TABLE_NAME + "(" + COL_ID + "INTEGER PRIMARY KEY," + COL_NAME
+ "TEXT," + COL_TELP + "TELP," + COL_REK + "TEXT" + ")";
db.execSQL(CREATE_CONTACT_TABLE);
}
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int NewVersion){
db.execSQL("DROP TABLE IF EXIST" + TABLE_NAME);
onCreate(db);
}
void addContact (Contact contact){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_NAME, contact.getName());
values.put(COL_TELP, contact.getTelp());
values.put(COL_REK, contact.getRek());
db.insert(TABLE_NAME, null, values);
db.close();
}
Contact getContact (int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[] { COL_ID, COL_NAME, COL_TELP, COL_REK}, COL_ID + "=?",
new String[] {String.valueOf(id) }, null, null, null, null);
if(cursor != null){
cursor.moveToFirst();
}
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3));
return contact;
}
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
Contact contact = new Contact();
contact.setId(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setTelp(cursor.getString(2));
contact.setRek(cursor.getString(3));
contactList.add(contact);
}while(cursor.moveToNext());
}
return contactList;
}
public int getContactsCount(){
String countQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
return cursor.getCount();
}}
答案 0 :(得分:1)
永远不会忘记sql关键字和表/列名称之间的空格:
CREATE TABLEcontact
或者你认为这可以执行吗?
{{1}}
答案 1 :(得分:0)
我发现你在创建表格时犯了一个错误,你把COL_TELP定义为TELP而不是文本作为输入参数我认为
答案 2 :(得分:0)
将数据库版本从1更改为2.