我已经在互联网上进行了 15-20 小时的研究,但仍然找不到这个问题的答案“如何在 Android 中使用 ListView 读取和更新 SQLite 数据库?” 换句话说,如何将 ListView 项目意图设置为 SQLite 数据库中保存的数据,以及如何在进入其他活动后更新它们。我已经尝试了许多不同的方法来做到这一点,到目前为止,它们要么不起作用,要么使我的应用程序崩溃。有人可以帮助或至少将我引向正确的方向,谢谢。
activity_main_activity_planes.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:orientation="vertical"
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=".MainActivityPlanes" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/add_button" />
</RelativeLayout>
activity_main_activity_paper.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=".MainActivityPaper" >
<EditText
android:id="@+id/editText1"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText3"
android:ems="10" >
</EditText>
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText3"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/save_button" />
<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText3"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/update_button" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="text" >
</EditText>
</RelativeLayout>
MainActivityPlanes.java
public class MainActivityPlanes extends Activity {
Button Add;
ListView List;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main_activity_planes);
Add = (Button) findViewById(R.id.button1);
List = (ListView) findViewById(R.id.listView1);
List.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
Intent launchActivity = new Intent(MainActivityPlanes.this, MainActivityChampagne.class);
startActivity(launchActivity);
}
});
Add.setOnClickListener(new OnClickListener () {
public void onClick (View v) {
Intent intent = new Intent (v.getContext(), MainActivityPaper.class);
startActivityForResult(intent, 0);
}
});
MainActivityChampagne info = new MainActivityChampagne(this);
info.open();
ArrayList<String> data = info.getData();
info.close();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, data);
List.setAdapter(arrayAdapter);
}
}
MainActivityPaper.java
public class MainActivityPaper extends Activity implements OnClickListener {
TextView text1, title1;
Button save1, update1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main_activity_paper);
text1 = (TextView) findViewById(R.id.editText1);
title1 = (TextView) findViewById(R.id.editText3);
save1 = (Button) findViewById(R.id.button3);
update1 = (Button) findViewById(R.id.button5);
save1.setOnClickListener(this);
update1.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.button3:
String titles = title1.getText().toString();
String notes = text1.getText().toString();
MainActivityChampagne entry = new MainActivityChampagne(MainActivityPaper.this);
entry.open();
entry.createEntry(titles, notes);
entry.close();
break;
case R.id.button5:
String thetitles = title1.getText().toString();
String thenotes = text1.getText().toString();
MainActivityChampagne up = new MainActivityChampagne(MainActivityPaper.this);
up.open();
up.updateEntry(thetitles, thenotes);
up.close();
break;
}
}
}
MainActivityChampagne.java
public class MainActivityChampagne {
public static final String KEY_NAME = "title";
public static final String KEY_NOTES = "lastcall";
private static final String DATABASE_NAME = "Champagnedb";
private static final String DATABASE_TABLE = "champagneTable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub\
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_NAME + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
onCreate(db);
}
}
public MainActivityChampagne(Context c){
ourContext = c;
}
public MainActivityChampagne open(){
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String titles, String notes) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, titles);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public ArrayList<String> getData() {
//TODO Auto-generated method stub
String[] columns = new String[] { KEY_NAME };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iNotes = c.getColumnIndex(KEY_NAME);
ArrayList<String> string_array = new ArrayList<String>();
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
result = c.getString (iNotes) + "\n";
string_array.add(result);
}
return string_array;
}
public void updateEntry(String thetitles, String thenotes) {
// TODO Auto-generated method stub
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_NAME, thetitles);
cvUpdate.put(KEY_NOTES, thenotes);
ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "=" + thetitles + KEY_NOTES + "=" + thenotes, null);
}
}
日志猫
06-21 03:42:16.782: E/Trace(3639): error opening trace file: No such file or directory (2)
06-21 03:42:18.622: D/dalvikvm(3639): GC_FOR_ALLOC freed 46K, 7% free 2545K/2712K, paused 440ms, total 444ms
06-21 03:42:18.715: I/dalvikvm-heap(3639): Grow heap (frag case) to 3.673MB for 1127536-byte allocation
06-21 03:42:19.022: D/dalvikvm(3639): GC_FOR_ALLOC freed 1K, 5% free 3645K/3816K, paused 308ms, total 308ms
06-21 03:42:19.312: D/dalvikvm(3639): GC_CONCURRENT freed <1K, 5% free 3651K/3816K, paused 18ms+15ms, total 290ms
06-21 03:42:20.582: I/Choreographer(3639): Skipped 65 frames! The application may be doing too much work on its main thread.
06-21 03:42:20.752: D/gralloc_goldfish(3639): Emulator without GPU emulation detected.
06-21 03:42:21.352: I/Choreographer(3639): Skipped 33 frames! The application may be doing too much work on its main thread.
06-21 03:42:24.841: D/dalvikvm(3639): GC_FOR_ALLOC freed 25K, 4% free 3984K/4132K, paused 67ms, total 95ms
06-21 03:42:24.941: D/dalvikvm(3639): GC_FOR_ALLOC freed 10K, 4% free 4238K/4400K, paused 61ms, total 67ms
06-21 03:42:25.081: D/dalvikvm(3639): GC_CONCURRENT freed <1K, 4% free 4766K/4936K, paused 74ms+6ms, total 124ms
06-21 03:42:25.094: D/dalvikvm(3639): WAIT_FOR_CONCURRENT_GC blocked 37ms
06-21 03:42:25.193: D/dalvikvm(3639): GC_FOR_ALLOC freed 0K, 4% free 5294K/5472K, paused 57ms, total 57ms
06-21 03:42:25.293: D/dalvikvm(3639): GC_FOR_ALLOC freed <1K, 4% free 6086K/6276K, paused 57ms, total 57ms
06-21 03:42:25.453: I/Choreographer(3639): Skipped 77 frames! The application may be doing too much work on its main thread.
06-21 03:42:32.381: D/AndroidRuntime(3639): Shutting down VM
06-21 03:42:32.401: W/dalvikvm(3639): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-21 03:42:32.461: E/AndroidRuntime(3639): FATAL EXCEPTION: main
06-21 03:42:32.461: E/AndroidRuntime(3639): android.content.ActivityNotFoundException: Unable to find explicit activity class {android.application.project.planes/android.application.project.planes.MainActivityChampagne}; have you declared this activity in your AndroidManifest.xml?
06-21 03:42:32.461: E/AndroidRuntime(3639): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
06-21 03:42:32.461: E/AndroidRuntime(3639): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
06-21 03:42:32.461: E/AndroidRuntime(3639): at android.app.Activity.startActivityForResult(Activity.java:3370)
06-21 03:42:32.461: E/AndroidRuntime(3639): at android.app.Activity.startActivityForResult(Activity.java:3331)
06-21 03:42:32.461: E/AndroidRuntime(3639): at android.app.Activity.startActivity(Activity.java:3566)
06-21 03:42:32.461: E/AndroidRuntime(3639): at android.app.Activity.startActivity(Activity.java:3534)
06-21 03:42:32.461: E/AndroidRuntime(3639): at android.application.project.planes.MainActivityPlanes$1.onItemClick(MainActivityPlanes.java:36)
06-21 03:42:32.461: E/AndroidRuntime(3639): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
06-21 03:42:32.461: E/AndroidRuntime(3639): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
06-21 03:42:32.461: E/AndroidRuntime(3639): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
06-21 03:42:32.461: E/AndroidRuntime(3639): at android.widget.AbsListView$1.run(AbsListView.java:3423)
06-21 03:42:32.461: E/AndroidRuntime(3639): at android.os.Handler.handleCallback(Handler.java:725)
06-21 03:42:32.461: E/AndroidRuntime(3639): at android.os.Handler.dispatchMessage(Handler.java:92)
06-21 03:42:32.461: E/AndroidRuntime(3639): at android.os.Looper.loop(Looper.java:137)
06-21 03:42:32.461: E/AndroidRuntime(3639): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-21 03:42:32.461: E/AndroidRuntime(3639): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 03:42:32.461: E/AndroidRuntime(3639): at java.lang.reflect.Method.invoke(Method.java:511)
06-21 03:42:32.461: E/AndroidRuntime(3639): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-21 03:42:32.461: E/AndroidRuntime(3639): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-21 03:42:32.461: E/AndroidRuntime(3639): at dalvik.system.NativeStart.main(Native Method)
06-21 03:42:35.471: I/Process(3639): Sending signal. PID: 3639 SIG: 9
06-21 03:43:13.141: D/dalvikvm(3658): GC_FOR_ALLOC freed 49K, 7% free 2545K/2716K, paused 229ms, total 232ms
06-21 03:43:13.161: I/dalvikvm-heap(3658): Grow heap (frag case) to 3.673MB for 1127536-byte allocation
06-21 03:43:13.241: D/dalvikvm(3658): GC_FOR_ALLOC freed 1K, 5% free 3645K/3820K, paused 74ms, total 74ms
06-21 03:43:13.391: D/dalvikvm(3658): GC_CONCURRENT freed <1K, 5% free 3645K/3820K, paused 10ms+4ms, total 152ms
06-21 03:43:14.711: I/Choreographer(3658): Skipped 78 frames! The application may be doing too much work on its main thread.
06-21 03:43:14.852: D/gralloc_goldfish(3658): Emulator without GPU emulation detected.
06-21 03:43:19.123: D/AndroidRuntime(3658): Shutting down VM
06-21 03:43:19.131: W/dalvikvm(3658): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-21 03:43:19.181: E/AndroidRuntime(3658): FATAL EXCEPTION: main
06-21 03:43:19.181: E/AndroidRuntime(3658): android.content.ActivityNotFoundException: Unable to find explicit activity class {android.application.project.planes/android.application.project.planes.MainActivityChampagne}; have you declared this activity in your AndroidManifest.xml?
06-21 03:43:19.181: E/AndroidRuntime(3658): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
06-21 03:43:19.181: E/AndroidRuntime(3658): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
06-21 03:43:19.181: E/AndroidRuntime(3658): at android.app.Activity.startActivityForResult(Activity.java:3370)
06-21 03:43:19.181: E/AndroidRuntime(3658): at android.app.Activity.startActivityForResult(Activity.java:3331)
06-21 03:43:19.181: E/AndroidRuntime(3658): at android.app.Activity.startActivity(Activity.java:3566)
06-21 03:43:19.181: E/AndroidRuntime(3658): at android.app.Activity.startActivity(Activity.java:3534)
06-21 03:43:19.181: E/AndroidRuntime(3658): at android.application.project.planes.MainActivityPlanes$1.onItemClick(MainActivityPlanes.java:36)
06-21 03:43:19.181: E/AndroidRuntime(3658): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
06-21 03:43:19.181: E/AndroidRuntime(3658): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
06-21 03:43:19.181: E/AndroidRuntime(3658): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
06-21 03:43:19.181: E/AndroidRuntime(3658): at android.widget.AbsListView$1.run(AbsListView.java:3423)
06-21 03:43:19.181: E/AndroidRuntime(3658): at android.os.Handler.handleCallback(Handler.java:725)
06-21 03:43:19.181: E/AndroidRuntime(3658): at android.os.Handler.dispatchMessage(Handler.java:92)
06-21 03:43:19.181: E/AndroidRuntime(3658): at android.os.Looper.loop(Looper.java:137)
06-21 03:43:19.181: E/AndroidRuntime(3658): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-21 03:43:19.181: E/AndroidRuntime(3658): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 03:43:19.181: E/AndroidRuntime(3658): at java.lang.reflect.Method.invoke(Method.java:511)
06-21 03:43:19.181: E/AndroidRuntime(3658): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-21 03:43:19.181: E/AndroidRuntime(3658): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-21 03:43:19.181: E/AndroidRuntime(3658): at dalvik.system.NativeStart.main(Native Method)
06-21 03:43:24.262: I/Process(3658): Sending signal. PID: 3658 SIG: 9
ProjectPlanes 清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.application.project.planes"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="android.application.project.planes.MainActivityPlanes"
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="android.application.project.planes.MainActivityPaper"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
更新了 LogCat
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.View.measure(View.java:15518)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.View.measure(View.java:15518)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.View.measure(View.java:15518)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-21 05:12:14.752: E/AndroidRuntime(4313): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.View.measure(View.java:15518)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.Choreographer.doFrame(Choreographer.java:532)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.os.Handler.handleCallback(Handler.java:725)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.os.Handler.dispatchMessage(Handler.java:92)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.os.Looper.loop(Looper.java:137)
06-21 05:12:14.752: E/AndroidRuntime(4313): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-21 05:12:14.752: E/AndroidRuntime(4313): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 05:12:14.752: E/AndroidRuntime(4313): at java.lang.reflect.Method.invoke(Method.java:511)
06-21 05:12:14.752: E/AndroidRuntime(4313): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-21 05:12:14.752: E/AndroidRuntime(4313): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-21 05:12:14.752: E/AndroidRuntime(4313): at dalvik.system.NativeStart.main(Native Method)
06-21 05:12:19.072: I/Process(4313): Sending signal. PID: 4313 SIG: 9