문제

The Problem

CursorLoader instantiation seems to be using URI of previous CursorLoader, rather than the URI passed in as an argument.

CursorLoader cursor = new CursorLoader(this, FLASHCARD_CONTENT_URI, 
FLASHCARD_FROM, null, null, null);

is giving the following error:

no such column: front (code 1): , while compiling: 
SELECT _ID, front, back, FROM deck

The Classes

  1. DeckIndex.java - responsible for querying the database "deck" table and returning a Cursor array of decks.
  2. DeckShow.java - responsible for querying the database "flashcard" table and returning a Cursor array of flashcards.
  3. AuducardOpenHelper.java - responsible for DDL of database and creating.
  4. DecksProvider - responsible for CRUD of "deck" table
  5. FlashcardsProvider - responsible for CRUD of "flashcard" table

Explanation

URI being passed:

public static final Uri FLASHCARD_CONTENT_URI = 
Uri.parse("content://" + AUTHORITY + "/" + FLASHCARD_TABLE_NAME);

I know for a fact that this is being processed by the DecksProvider and not the FlashcardsProvider.

And the UriMatcher in FlashcardsProvider:

@Override
public boolean onCreate(){
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(AUTHORITY, "flashcard", FLASHCARDS);

    auducards = new AuducardOpenHelper(getContext());
    return true;
}

I think it is important to mention that the DecksIndex class is called first, and thus a call to the DeckProvider is completed successfully. It is not until I click on one of the decks (which starts the DeckShow activity) when the DeckShow onCreate method is called:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLoaderManager().initLoader(1, null, this);
        setContentView(R.layout.activity_deck_show);
        mAdapter = new SimpleCursorAdapter(this, R.layout.flashcard_item, null, 
              FLASHCARD_FROM, FLASHCARD_TO, 0 );

        Intent intent = getIntent();
        String message = intent.getStringExtra(DeckIndex.EXTRA_MESSAGE);

    }

and the error occurs.

LoaderManager init lines:

DeckIndex.java

getLoaderManager().initLoader(0, null, this);

DeckShow.java

getLoaderManager().initLoader(1, null, this);

Thank you for looking at my question and let me know if any additional details or code will help!

도움이 되었습니까?

해결책

I've had this problem and learned that in order to add a second content provider, I had to:

  • Define the second provider in the Androidmanifest.xml file. Set the android:authority to the full name of the package and class.
  • Use the authority as the URL

The authority for each provider must be unique and the URL for each provider must match the authority. The URL must be all lowercase as well.

Here is a hypothetical configuration:

AndroidManifest.xml

<application>
  <provider android:name="com.acme.ProviderOne"
    android:authorities="com.acme.providerone" />
  <provider android:name="com.acme.ProviderTwo"
    android:authorities="com.acme.providertwo" />
</application

src/com/acme/providerone.java

package com.acme

import android.content.ContentProvider;

public class ProviderOne extends ContentProvider {
  public static final Uri CONTENT_URI = Uri.parse("content://com.acme.providerone/objects");
  private static final int ALLROWS = 1;
  private static final int SINGLE_ROW = 2;

  static {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI("com.acme.providerone", "objects", ALLROWS);
    uriMatcher.addURI("com.acme.providerone", "objects/#", SINGLE_ROW);
  }
}

src/com/acme/providertwo.java

package com.acme

import android.content.ContentProvider;

public class ProviderTwo extends ContentProvider {
  public static final Uri CONTENT_URI = Uri.parse("content://com.acme.providertwo/objects");
  private static final int ALLROWS = 1;
  private static final int SINGLE_ROW = 2;

  static {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI("com.acme.providertwo", "objects", ALLROWS);
    uriMatcher.addURI("com.acme.providertwo", "objects/#", SINGLE_ROW);
  }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top