活动未显示数据库信息

时间:2019-04-18 07:19:25

标签: java android sqlite

我试图在我的应用程序中实现sqlite数据库。添加新条目有效,但显示不起作用。我定义了一个ContentProvider,一个DbHelper,一个游标适配器,但是某处似乎有一个错误。 数据库具有三列(ID,名称和日期)

主要活动(应在其中显示数据)

@Override
protected void onStart() {
    super.onStart();
    displayDatabaseInfo();
}

private void displayDatabaseInfo() {

    String[] projection = {
            EventEntry._ID,
            EventEntry.COLUMN_EVENT_NAME,
            EventEntry.COLUMN_EVENT_DATE,
    };

    Cursor cursor = getContentResolver().query(
            EventEntry.CONTENT_URI,
            projection,
            null,
            null,
            null);

    ListView eventsLV = findViewById(R.id.events_lv);
    EventCursorAdapter mCursorAdapter;
    mCursorAdapter = new EventCursorAdapter(this, **cursor**);
    eventsLV.setAdapter(mCursorAdapter);

CursorAdapter

public class EventCursorAdapter extends CursorAdapter {

public EventCursorAdapter(Context context, Cursor c){
    super(context, c, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.event_item, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    TextView eventNameTV = view.findViewById(R.id.event_item_name_tv);
    TextView eventDateTV = view.findViewById(R.id.event_item_date_tv);

    // Extract properties from cursor
    int eventNameIndex = cursor.getColumnIndex(EventContract.EventEntry.COLUMN_EVENT_NAME);
    int eventDateIndex = cursor.getColumnIndex(EventContract.EventEntry.COLUMN_EVENT_DATE);

    String eventNameString = cursor.getString(eventNameIndex);
    String eventDateString = cursor.getString(eventDateIndex);

    // Populate fields with extracted properties
    eventNameTV.setText(eventNameString);
    eventDateTV.setText(eventDateString);
}
  }

合同类别:

public final class EventContract {

private EventContract (){}

public static final String CONTENT_AUTHORITY = "com.example.deadline";
public static final String PATH_EVENTS = "events";
public static final Uri BASE_CONTENT_URI = Uri.parse("context://" + CONTENT_AUTHORITY);

public static final class EventEntry implements BaseColumns{

    public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_EVENTS);
    public static final String CONTENT_LIST_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY
            + "/" + PATH_EVENTS;
    public final static String TABLE_NAME = "events";

    public final static String _ID = BaseColumns._ID;
    public final static String COLUMN_EVENT_NAME = "name";
    public final static String COLUMN_EVENT_DATE = "date";

    public final static String TABLE_SORT_BY = "name ASC";

}

和ContentProvider:

public class EventProvider extends ContentProvider {


private static final int EVENTS = 100;
private static final int EVENT_ID = 101;

private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

static {
    sUriMatcher.addURI(EventContract.CONTENT_AUTHORITY, EventContract.PATH_EVENTS, EVENTS);
    sUriMatcher.addURI(EventContract.CONTENT_AUTHORITY, EventContract.PATH_EVENTS + "/#", EVENT_ID);
}

public static final String LOG_TAG = EventProvider.class.getSimpleName();

private EventDbHelper mDbHelper = new EventDbHelper(getContext());

@Override
public boolean onCreate() {
    mDbHelper = new EventDbHelper(getContext());
    return true;
}

@Override
public Cursor query(Uri uri, String[] projection,  String selection, String[] selectionArgs, String sortOrder) {
    SQLiteDatabase database = mDbHelper.getReadableDatabase();

    // This cursor will hold the result of the query
    Cursor cursor;

    // Figure out if the URI matcher can match the URI to a specific code
    int match = sUriMatcher.match(uri);
    switch (match) {
        case EVENTS:

            cursor = database.query(EventEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
            break;
        case EVENT_ID:

            selection = EventEntry._ID + "=?";
            selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};

            cursor = database.query(EventEntry.TABLE_NAME, projection, selection, selectionArgs,
                    null, null, EventEntry.TABLE_SORT_BY);
            break;
        default:
            throw new IllegalArgumentException("Cannot query unknown URI " + uri);
    }
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}

1 个答案:

答案 0 :(得分:0)

更改此行

abc.com/message

mCursorAdapter = new EventCursorAdapter(this, null);