jeudi 8 octobre 2015

Why do I get an empty listview with contentprovider and loader?

Maybe a to simple Question, but I can't find a solution. When I'm testing the following code, the listview is always empty, but the SQLite database isn't empty and it's not giving any exceptions. I have looked for any solutions here already, but nothing helped. Probably the mistake is in the loader callbacks but I don't have any ideas anymore.

Here the code:

The activity with adapter and loader callbacks:

// Attributes

private static final int CURSOR_LOADER_ID = 0x01;
// Views
private ListView navigationList;

// Fragments
AddListDialog addListDialog;
// Adapter
private SimpleCursorAdapter adapter;


//==============================================================================================
//                                  Overridden Method's
//==============================================================================================

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView (R.layout.activity_mainnavigation);

    // Implementation
    navigationList = (ListView) findViewById (R.id.navigationList);

    String [] from = {ListContract.KEY_NAME};
    int [] to = {R.id.listItem};

    adapter = new SimpleCursorAdapter (
            this, R.layout.layout_listitem,
            null, from, to, 0);

    navigationList.setAdapter (adapter);

    getSupportLoaderManager().initLoader(CURSOR_LOADER_ID, null, this);

} // protected void onCreate (Bundle savedInstanceState)

@Override
public void onClick (View v) {

    switch (v.getId()) {

        case R.id.addButton:
            Log.i("Safe-MainNavigationAct", "Add-Button is touched!");
            this.showAddDialog ();
            break;
        // case R.id.addButton:

    } // switch (v.getId())

} // public void onClick (View v)

@Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {

}

@Override
public Loader <Cursor> onCreateLoader (int id, Bundle args) {
    String [] projection = {ListContract.KEY_ID, ListContract.KEY_NAME};
    CursorLoader cursorLoader = new CursorLoader (this, SafeProvider.LISTS_URI, projection,
            null, null, null);
    return cursorLoader;
} // public Loader <Cursor> onCreateLoader (int id, Bundle args)

@Override
public void onLoadFinished (Loader <Cursor> loader, Cursor data) {
    adapter.swapCursor (data);
} // public void onLoadFinished (Loader <Cursor> loader, Cursor data)

@Override
public void onLoaderReset (Loader <Cursor> loader) {
    adapter.swapCursor (null);
} // public void onLoaderReset (Loader <Cursor> loader)

@Override
public void onFinishAddListDialog (String input) {
    ContentValues values = new ContentValues ();
    values.putNull(ListContract.KEY_ID);
    values.put(ListContract.KEY_NAME, input);
    getContentResolver().insert(SafeProvider.LISTS_URI, values);
    Toast.makeText (this, "The name of the new List is " + input, Toast.LENGTH_LONG).show();
} // public void onFinishAddListDialog (String input)

@Override
protected void onResume () {
    super.onResume ();
    getSupportLoaderManager ().restartLoader (CURSOR_LOADER_ID, null, this);
} // protected void onResume ()

private void showAddDialog () {
    addListDialog = new AddListDialog ();
    FragmentManager fragmentManager = getSupportFragmentManager ();
    addListDialog.show(fragmentManager, "Add_List_fragment");
    Log.i("Safe-MainNavigationAct", "Content replaced by AddListDialog");
} // private void showAddDialog ()

private void displayList () {

}

The contentprovider:

private static final String AUTHORITY = "de.schmidt.android.safe.contentproviderclasses";

public static final int LISTS = 100;
public static final int PASSWORDS = 101;

public static final int LISTS_ID = 110;
public static final int PASSWORDS_ID = 111;

private static final String LISTS_BASE_PATH = "lists";
private static final String PASSWORDS_BASE_PATH = "passwords";

public static final Uri LISTS_URI = Uri.parse("content://" + AUTHORITY + "/" + LISTS_BASE_PATH);
public static final Uri PASSWORDS_URI
        = Uri.parse("content://" + AUTHORITY + "/" + PASSWORDS_BASE_PATH);

private static final UriMatcher uriMatcher = new UriMatcher (UriMatcher.NO_MATCH);
static {
    uriMatcher.addURI (AUTHORITY, LISTS_BASE_PATH, LISTS);
    uriMatcher.addURI (AUTHORITY, PASSWORDS_BASE_PATH, PASSWORDS);
    uriMatcher.addURI (AUTHORITY, LISTS_BASE_PATH + "/#", LISTS_ID);
    uriMatcher.addURI (AUTHORITY, PASSWORDS_BASE_PATH + "/#", PASSWORDS_ID);
}

private SafeDataBase dataBase;

//==============================================================================================
//                                  Overridden Method's
//==============================================================================================

@Override
public boolean onCreate () {
    dataBase = new SafeDataBase (getContext());
    return true;
} // public boolean onCreate ()

@Nullable
@Override
public Cursor query (Uri uri, String[] projection, String selection,
                     String[] selectionArgs, String sortOrder) {
    SQLiteDatabase db = dataBase.getReadableDatabase();
    SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder ();
    Cursor cursor = null;
    int uriType = uriMatcher.match (uri);
    switch (uriType) {

        case LISTS_ID:
            qBuilder.setTables(ListContract.TABLE_LIST);
            qBuilder.appendWhere (ListContract.KEY_ID + "=" + uri.getLastPathSegment ());
            cursor = qBuilder.query (db, projection, selection,
                    selectionArgs, null, null, sortOrder);
            cursor.setNotificationUri (getContext ().getContentResolver (), uri);
            return cursor;
        // case LISTS_ID:

        case PASSWORDS_ID:
            qBuilder.setTables(PassContract.TABLE_PASS);
            qBuilder.appendWhere(PassContract.KEY_PASS_ID + "=" + uri.getLastPathSegment());
            cursor = qBuilder.query (db, projection, selection,
                    selectionArgs, null, null, sortOrder);
            cursor.setNotificationUri (getContext ().getContentResolver (), uri);
            return cursor;
        // case PASSWORDS_ID:

        case LISTS:
            break;
        // case LISTS:

        case PASSWORDS:
            break;
        // case PASSWORDS:

        default:
            throw new IllegalArgumentException ("Unknown URI");
        // default:

    } // switch (uriType)
    return null;
} // public Cursor query (Uri uri, String[] projection, String selection,
                    // String[] selectionArgs, String sortOrder)

@Nullable
@Override
public String getType (Uri uri) {
    return null;
} // public String getType (Uri uri)

@Nullable
@Override
public Uri insert (Uri uri, ContentValues values) {
    int uriType = uriMatcher.match (uri);
    SQLiteDatabase db = dataBase.getWritableDatabase();
    long newID = 0;
    switch (uriType) {

        case LISTS:
            newID = db.insertOrThrow (ListContract.TABLE_LIST, null, values);

            if (newID > 0) {
                Uri newUri = ContentUris.withAppendedId (uri, newID);
                getContext ().getContentResolver ().notifyChange (uri, null);
                return newUri;
            } // if ( newID > 0)
            else {
                try {
                    throw new SQLException ("Failed to insert row into " + uri);
                } // try
                catch (SQLException e) {
                    e.printStackTrace();
                } // catch (SQLException e)
            } // else
            break;
        // case LISTS:

        case PASSWORDS:
            newID = db.insert (PassContract.TABLE_PASS, null, values);
            if (newID > 0) {
                Uri newUri = ContentUris.withAppendedId (uri, newID);
                getContext ().getContentResolver ().notifyChange (uri, null);
                return newUri;
            } // if ( newID > 0)
            else {
                try {
                    throw new SQLException ("Failed to insert row into " + uri);
                } // try
                catch (SQLException e) {
                    e.printStackTrace();
                } // catch (SQLException e)
            } // else
            break;
        // case PASSWORDS:

        default:
            throw new IllegalArgumentException ("Invalid URI for insert");
        // default:

    } // switch (uriType)
    return null;
} // public Uri insert (Uri uri, ContentValues values)

@Override
public int delete (Uri uri, String selection, String[] selectionArgs) {
    int rowsAffected = 0;
    int uriType = uriMatcher.match (uri);
    SQLiteDatabase db = dataBase.getWritableDatabase ();
    switch (uriType) {

        case LISTS:
            rowsAffected = db.delete (ListContract.TABLE_LIST, selection, selectionArgs);
            break;
        // case LISTS:

        case LISTS_ID:
            String id_list = uri.getLastPathSegment ();
            if (TextUtils.isEmpty (selection)) {
                rowsAffected = db.delete (ListContract.TABLE_LIST, ListContract.KEY_ID
                        + "=" + id_list, null);
            } // if (TextUtils.isEmpty (selection))
            else {
                rowsAffected = db.delete (ListContract.TABLE_LIST, selection + "and"
                        + ListContract.KEY_ID + "=" + id_list, selectionArgs);
            } // else
            break;
        //case LISTS_ID:

        case PASSWORDS:
            rowsAffected = db.delete (PassContract.TABLE_PASS, selection, selectionArgs);
            break;
        // case PASSWORDS:

        case PASSWORDS_ID:
            String id_pass = uri.getLastPathSegment ();
            if (TextUtils.isEmpty (selection)) {
                rowsAffected = db.delete (PassContract.TABLE_PASS, PassContract.KEY_PASS_ID
                        + "=" + id_pass, null);
            } // (TextUtils.isEmpty (selection))
            else {
                rowsAffected = db.delete (PassContract.TABLE_PASS, selection + "and"
                        + PassContract.KEY_PASS_ID + "=" + id_pass, selectionArgs);
            } // else
            break;
        // case PASSWORDS_ID:

        default:
            throw new IllegalArgumentException ("Unknown or invalid URI, " + uri);

    } // switch (uriType)
    getContext ().getContentResolver ().notifyChange (uri, null);
    return rowsAffected;
} // public int delete (Uri uri, String selection, String[] selectionArgs)

@Override
public int update (Uri uri, ContentValues values,
                   String selection, String[] selectionArgs) {
    SQLiteDatabase db = dataBase.getWritableDatabase ();
    int rowsAffected = 0;
    int uriType = uriMatcher.match (uri);
    switch (uriType) {
        case LISTS:
            rowsAffected = db.update (ListContract.TABLE_LIST, values,
                    selection, selectionArgs);
            break;
        // case LISTS:

        case LISTS_ID:
            String id_list = uri.getLastPathSegment ();
            StringBuilder modSelection_list = new StringBuilder (ListContract.KEY_ID
                    + "=" + id_list);
            if (!TextUtils.isEmpty(selection)) {
                modSelection_list.append (" and " + selection);
            } // if ( !TextUtils.isEmpty(selection))

            rowsAffected = db.update (ListContract.TABLE_LIST, values,
                    modSelection_list.toString (), null);
            break;
        // case LISTS_ID:

        case PASSWORDS:
            rowsAffected = db.update (PassContract.TABLE_PASS, values,
                    selection, selectionArgs);
            break;
        // case PASSWORDS:

        case PASSWORDS_ID:
            String id_pass = uri.getLastPathSegment ();
            StringBuilder modeSelection_pass = new StringBuilder (PassContract.KEY_PASS_ID
                    + "=" + id_pass);
            if (!TextUtils.isEmpty(selection)) {
                modeSelection_pass.append (" and " + selection);
            } // if (!TextUtils.isEmpty(selection))

            rowsAffected = db.update (PassContract.TABLE_PASS, values,
                    modeSelection_pass.toString (), null);
            break;
        // case PASSWORDS_ID:

        default:
            throw new IllegalArgumentException ("Unknown URI");
        // default:

    } // switch (uriType)
    getContext ().getContentResolver ().notifyChange (uri, null);
    return rowsAffected;
} // public int update (Uri uri, ContentValues values,
                // String selection, String[] selectionArgs)

The ID's from xml-files are correct, i have checked that already.

In advance, thanks for help.

Aucun commentaire:

Enregistrer un commentaire