Как мне написать код "получить по индексу", полученный из комнаты БД, поставщику контента? Вот мой код (FavoriteContentProvider. java) - PullRequest
0 голосов
/ 01 августа 2020

Это мой поставщик содержимого (FavoriteContentProvider. java)

Как мне поставить условие «получить по индексу» в поставщик содержимого => эту строку оператора (if (code == CODE_DIR || code = = CODE_ITEM) {final Context context = getContext ();) из источников Favorite Entity и Favorite Dao?

private static final int CODE_DIR = 1;
private static final int CODE_ITEM = 2;

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

static {
    MATCHER.addURI(AUTHORITY, FavoriteEntity.TABLE_NAME, CODE_DIR);
    MATCHER.addURI(AUTHORITY, FavoriteEntity.TABLE_NAME + "/*", CODE_ITEM);
}

@Override
public boolean onCreate() {
    FavoriteRoomDatabase mFavoriteRoomDatabase = FavoriteRoomDatabase.getDatabase(getContext());
    return true;
}

@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    final int code = MATCHER.match(uri);
    if (code == CODE_DIR || code == CODE_ITEM){
        final Context context = getContext();

        if (context == null) {
            return null;
        }

        FavoriteDao mFavoriteDao = FavoriteRoomDatabase.getDatabase(context).favoriteDao();
        Cursor cursor = null;

        if (code == CODE_DIR) {
            cursor = mFavoriteDao.selectAll();
        }
        Objects.requireNonNull(cursor).setNotificationUri(context.getContentResolver(), uri);
        return cursor;

    }else {
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }
}

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

@Override
public Uri insert(@NonNull Uri uri, ContentValues values) {
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
    return 0;
}

И это моя любимая комната Dao

@Query("SELECT * from "+ FavoriteEntity.TABLE_NAME +" ORDER BY "+ FavoriteEntity.COLUMN_ID +" ASC")
LiveData<List<FavoriteEntity>> getAllFavorite();

@Query("SELECT COUNT(*) FROM "+ FavoriteEntity.TABLE_NAME +" WHERE "+ FavoriteEntity.COLUMN_USER_ID +" == :userId")
LiveData<Integer> getCount(int userId);

@Query("SELECT * FROM " + FavoriteEntity.TABLE_NAME)
Cursor selectAll();

@Insert(onConflict = OnConflictStrategy.IGNORE)
void insert(FavoriteEntity mFavoriteEntity);

@Delete()
void delete(FavoriteEntity mFavoriteEntity);

@Query("Delete FROM "+ FavoriteEntity.TABLE_NAME +" where "+ FavoriteEntity.COLUMN_LOGIN +" LIKE  :login")
void deleteUserByLogin(String login);

и FavoriteEntity

public static final String TABLE_NAME = "favorite";

static final String COLUMN_ID = "id";

static final String COLUMN_USER_ID = "user_id";

static final String COLUMN_LOGIN = "login";

@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = COLUMN_ID )
private int id;

@ColumnInfo(name = COLUMN_USER_ID)
private int user_id;

@ColumnInfo(name = COLUMN_LOGIN)
private String login;

@ColumnInfo(name = "public_repos")
private int publicRepos;

@ColumnInfo(name = "followers")
private int followers;

@ColumnInfo(name = "following")
private int following;

@ColumnInfo(name = "name")
private String name;

@ColumnInfo(name = "company")
private String company;

@ColumnInfo(name = "blog")
private String blog;

@ColumnInfo(name = "location")
private String location;

public FavoriteEntity() {
}

protected FavoriteEntity(Parcel in) {
    id = in.readInt();
    user_id = in.readInt();
    login = in.readString();
    publicRepos = in.readInt();
    followers = in.readInt();
    following = in.readInt();
    name = in.readString();
    company = in.readString();
    blog = in.readString();
    location = in.readString();
}

public static final Creator<FavoriteEntity> CREATOR = new Creator<FavoriteEntity>() {
    @Override
    public FavoriteEntity createFromParcel(Parcel in) {
        return new FavoriteEntity(in);
    }

    @Override
    public FavoriteEntity[] newArray(int size) {
        return new FavoriteEntity[size];
    }
};

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getUser_id() {
    return user_id;
}

public void setUser_id(int user_id) {
    this.user_id = user_id;
}

public String getLogin() {
    return login;
}

public void setLogin(String login) {
    this.login = login;
}

public int getPublicRepos() {
    return publicRepos;
}

public void setPublicRepos(int publicRepos) {
    this.publicRepos = publicRepos;
}

public int getFollowers() {
    return followers;
}

public void setFollowers(int followers) {
    this.followers = followers;
}

public int getFollowing() {
    return following;
}

public void setFollowing(int following) {
    this.following = following;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getCompany() {
    return company;
}

public void setCompany(String company) {
    this.company = company;
}

public String getBlog() {
    return blog;
}

public void setBlog(String blog) {
    this.blog = blog;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(id);
    dest.writeInt(user_id);
    dest.writeString(login);
    dest.writeInt(publicRepos);
    dest.writeInt(followers);
    dest.writeInt(following);
    dest.writeString(name);
    dest.writeString(company);
    dest.writeString(blog);
    dest.writeString(location);
}
...