0

我对android(和编程)很陌生,所以我希望这个问题不会太愚蠢。我试图通过教程和通过 stackoverflow 搜索来回答我的问题,但没有成功。

我正在尝试使用 SQLite 数据库填充列表:当我单击列表上的某个项目时,我希望它也从列表视图和数据库中删除。通过调试,我认为问题出在我的 ArrayAdapter 方法上,因为即使像“getCount()”这样的基本方法似乎也不会产生输出。

对于我的 SQLite DatabaseHandler 类,这里是获取数据库中所有项目和删除项目的方法:

            // Getting All breadcrumbs
    public List<Breadcrumb> getAllBreadcrumbs() {
     List<Breadcrumb> breadcrumbList = new ArrayList<Breadcrumb>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_BREADCRUMBS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Breadcrumb breadcrumb = new Breadcrumb();
            breadcrumb.setId(Integer.parseInt(cursor.getString(0)));
            breadcrumb.setBreadcrumbLatitude(cursor.getInt(1));
            breadcrumb.setBreadcrumbLongitude(cursor.getInt(2));
            // Adding location to list
            breadcrumbList.add(breadcrumb);
        } while (cursor.moveToNext());
    }

    // Deleting single breadcrumb
      public void deleteBreadcrumb(Breadcrumb breadcrumb) {
        SQLiteDatabase db = this.getWritableDatabase();
        int id = breadcrumb.getId();
        System.out.println("Comment deleted with id: " + id);
        db.delete(TABLE_BREADCRUMBS, KEY_ID
                + " = " + id, null);
        db.close();
          }

麻烦活动的 XML 布局:

<LinearLayout 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=".CollectedBreadcrumbsActivity" >

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

这是活动:

public class CollectedBreadcrumbsActivity extends Activity implements OnItemClickListener {

private ListView listview;
private DatabaseHandler db;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_collected_breadcrumbs);

    DatabaseHandler db = new DatabaseHandler(this);
    List<Breadcrumb> breadcrumbs = db.getAllBreadcrumbs();

    listview = (ListView) findViewById(R.id.list);
    ArrayAdapter<Breadcrumb> adapter = new ArrayAdapter<Breadcrumb>(this,
            android.R.layout.simple_list_item_1, breadcrumbs);
        listview.setAdapter(adapter);
    listview.setOnItemClickListener(this);
}

public void onItemClick(AdapterView<?> l, View v, int position, long id) {
    Log.d("HelloListView", "You clicked Item: " + id + " at position:" + position);
    Log.d("HelloListView", "Number of items in adapter:" + listview.getAdapter().getCount());
    @SuppressWarnings("unchecked")
    ArrayAdapter<Breadcrumb> adapter = (ArrayAdapter<Breadcrumb>) listview.getAdapter();
    if (listview.getAdapter().getCount() > 0) {
        Breadcrumb breadcrumb = (Breadcrumb) listview.getAdapter().getItem(position);
        db.deleteBreadcrumb(breadcrumb);
        adapter.remove(breadcrumb);
        }
    adapter.notifyDataSetChanged();
    }
}

列表视图显示正常,但是当我单击列表中的某个项目时,应用程序崩溃并显示以下日志:

05-07 19:08:40.671: D/HelloListView(29562): You clicked Item: 5 at position:5
05-07 19:08:40.671: W/dalvikvm(29562): threadid=1: thread exiting with uncaught exception (group=0x40e4c438)
05-07 19:08:40.671: E/AndroidRuntime(29562): FATAL EXCEPTION: main
05-07 19:08:40.671: E/AndroidRuntime(29562): java.lang.NullPointerException
05-07 19:08:40.671: E/AndroidRuntime(29562):    at com.detimil.breadcrumbs1.CollectedBreadcrumbsActivity.onItemClick(CollectedBreadcrumbsActivity.java:42)
05-07 19:08:40.671: E/AndroidRuntime(29562):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-07 19:08:40.671: E/AndroidRuntime(29562):    at android.widget.AbsListView.performItemClick(AbsListView.java:1268)
05-07 19:08:40.671: E/AndroidRuntime(29562):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3059)
05-07 19:08:40.671: E/AndroidRuntime(29562):    at android.widget.AbsListView$1.run(AbsListView.java:3950)
05-07 19:08:40.671: E/AndroidRuntime(29562):    at android.os.Handler.handleCallback(Handler.java:615)
05-07 19:08:40.671: E/AndroidRuntime(29562):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 19:08:40.671: E/AndroidRuntime(29562):    at android.os.Looper.loop(Looper.java:137)
05-07 19:08:40.671: E/AndroidRuntime(29562):    at android.app.ActivityThread.main(ActivityThread.java:4950)
05-07 19:08:40.671: E/AndroidRuntime(29562):    at java.lang.reflect.Method.invokeNative(Native Method)
05-07 19:08:40.671: E/AndroidRuntime(29562):    at java.lang.reflect.Method.invoke(Method.java:511)
05-07 19:08:40.671: E/AndroidRuntime(29562):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-07 19:08:40.671: E/AndroidRuntime(29562):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-07 19:08:40.671: E/AndroidRuntime(29562):    at dalvik.system.NativeStart.main(Native Method)

同样,我认为问题一定出在我的 ArrayAdapter 上,因为Log.d("HelloListView", "Number of items in adapter:" + listview.getAdapter().getCount());在应用程序崩溃之前,即使此日志也不会产生任何内容。

如果有人对此有任何想法,我将非常感谢您的帮助。再次,对不起,如果这是一个愚蠢的问题。

非常感谢。

4

2 回答 2

4

db的是null

因为

  DatabaseHandler db = new DatabaseHandler(this);

它将创建一个本地实例但不实例化db字段所以让它像这样

db = new DatabaseHandler(this);
于 2013-05-08T05:34:36.703 回答
0

您的数据库应该在全局而不是本地启动。

我想补充一点,总体而言,使用 ArrayAdapter 从数据库加载并不是一个好主意。您应该使用 CursorAdapter 和 CursorLoader 来管理数据库。

这是关于自定义 CursorAdapter 的一个很好的教程:链接

CursorLoaders 的另一个:链接

于 2014-02-13T17:15:14.333 回答