我在sqlite中有一个数据库..它的DBAdapter类如下: package com.example.checkdb;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBAdapter {
static final String KEY_ROWID="_id";
static final String KEY_NAME="name";
static final String KEY_EMAIL="email";
static final String TAG="DBAdapter";
static final String DATABASE_NAME="MyDB";
static final String DATABASE_TABLE="contacts";
static final int DATABASE_VERSION=1;
static final String DATABASE_CREATE="create table contacts"
+"(_id integer primary key autoincrement,"
+"name text not null, email text not null);";
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
public DBAdapter(Context context) {
this.context=context;
DBHelper=new DatabaseHelper(context);
// TODO Auto-generated constructor stub
}
private static class DatabaseHelper extends SQLiteOpenHelper{
DatabaseHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
public DBAdapter open() throws SQLException{
db=DBHelper.getWritableDatabase();
return this;
}
public void close(){
DBHelper.close();
}
public long insertContact(String name,String email){
ContentValues initialValues= new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_EMAIL,email);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteContact(long rowId){
return db.delete(DATABASE_TABLE, KEY_ROWID+"="+rowId, null)>0;
}
public Cursor getAllContacts(){
return db.query(DATABASE_TABLE, new String[]{KEY_ROWID,KEY_NAME,KEY_EMAIL},
null, null, null, null, null);
}
}
我的主要活动包含添加和显示联系人的按钮。它遵循这里: package com.example.checkdb;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
public static DBAdapter db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=new DBAdapter(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onAdd(View v){
Intent i=new Intent(this,AddActivity.class);
startActivity(i);
}
public void onShow(View v){
Intent i=new Intent(this,ShowActivity.class);
startActivity(i);
}
}
Show活动如下:
package com.example.checkdb;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class ShowActivity extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
int i=0;
try{
ListView listView =(ListView)findViewById(R.id.list_contact);
MainActivity.db.open();
Cursor c=MainActivity.db.getAllContacts();
MainActivity.db.close();
String from[]=new String[]{DBAdapter.KEY_NAME};
int to[]=new int[]{R.id.text_View_Name};
SimpleCursorAdapter adapter;
adapter=new SimpleCursorAdapter(this,R.layout.row_layout,
c,from,to,i);
listView.setAdapter(adapter);
}
catch(Exception e){
Toast.makeText(this, "error",Toast.LENGTH_LONG).show();
}
}
}
然后是我的activity_show.xml,我的布局文件是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list_contact"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
每行的row_layout如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text_View_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
我的activity_main.xml文件如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:text="@string/add_butt"
android:onClick="onAdd" />
<Button
android:id="@+id/button_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button_add"
android:layout_below="@+id/button_add"
android:layout_marginTop="83dp"
android:text="@string/show_butt"
android:onClick="onShow" />
</RelativeLayout>
我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.checkdb"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.checkdb.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.checkdb.AddActivity"
android:label="@string/app_name" />
<activity
android:name="com.example.checkdb.ShowActivity"
android:label="@string/app_name" />
</application>
</manifest>
现在,当我点击“显示”按钮时,我遇到了问题。 模拟器关闭,不幸的是app已停止错误消息。 请帮帮我......