Question

I'm pretty new to android (and programming) so I hope this question isn't too stupid. I've tried to answer my question through tutorials and by searching through stackoverflow without success.

I'm trying to populate a list with an SQLite database: when I click an item on the list, I want it to delete from the listview and from the database, too. Through debugging, I think the problem is with the my ArrayAdapter methods, since even basic methods here like "getCount()" don't seem to produce outputs.

For my SQLite DatabaseHandler class, here is the method for getting all the items in the database and for deleting an item:

            // 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();
          }

The XML layout for the troublesome activity:

<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>

And here is the activity:

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();
    }
}

The listview shows up alright, but when I click an item on the list, the app crashes with the following log:

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)

Again, I think the problem must be with my ArrayAdapter because even this log Log.d("HelloListView", "Number of items in adapter:" + listview.getAdapter().getCount()); doesn't produce anything before the app crashes.

If anyone has any thoughts on this I would really appreciate the help. Again, sorry if this is a silly question.

Thank you very much.

Was it helpful?

Solution

Your db is null,

Because

  DatabaseHandler db = new DatabaseHandler(this);

it will create a local instance but doesn't instantiate db field so make it like this

db = new DatabaseHandler(this);

OTHER TIPS

Your db should be inititated globally not locally.

I would like to add that overall it's not a good idea to use ArrayAdapter to load from DB. You should use CursorAdapter and CursorLoader to manage database.

Here's a good tutorial on custom CursorAdapter: link

Another one for CursorLoaders: link

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top