gpt4 book ai didi

android - 为什么这个 Android Room 插件不起作用?

转载 作者:IT老高 更新时间:2023-10-28 13:43:08 25 4
gpt4 key购买 nike

我用空间实现了缓存,但由于某种原因它被破坏了,要么它没有插入,要么没有获取数据,虽然我已经做了很多调试,但仍然没有任何线索......有人可以帮忙吗?所以,图片如下:

主 Activity :

mArticleViewModel = ViewModelProviders.of(this).get(ArticleViewModel.class);
mArticleViewModel.getArticleList(mPageNumber).observe(this, articles
-> { /* doesn't matter */ });

View 模型:

public class ArticleViewModel extends AndroidViewModel {

public static final String TAG = "ArticleViewModel";
private LiveData<List<Article>> articleList;
private ArticleRepository articleRepository;

public ArticleViewModel(@NonNull Application application) {
super(application);
Log.d(TAG, "ArticleViewModel");
if (articleRepository == null) {
articleRepository = new ArticleRepository(application);
}
}


public LiveData<List<Article>> getArticleList(int pageNumber) {
Log.d(TAG, "getArticleList");
articleList = articleRepository.getArticles();
return articleList;
}
}

存储库:

public class ArticleRepository {
public static final String TAG = "ArticleRepository";
public static final int PAGE_SIZE = 20;

private ArticleDao mArticleDao;


public ArticleRepository(Application application) {
ArticleRoomDatabase db =
ArticleRoomDatabase.getInstance(application);
mArticleDao = db.articleDao();
loadArticles(1);
}

public void loadArticles(int page) {
Log.d(TAG, "getArticles");



// ApiService.getService().getArticles("test", "thumbnail", page,
PAGE_SIZE).enqueue(new Callback<Example>() {
// @Override
// public void onResponse(Call<Example> call,
Response<Example> response) {
// Log.d(TAG, "onResponse");
// if (response.isSuccessful()) {
// Log.d(TAG, "isSuccessful");
// List<Article> articles =
pojoToEntity(response.body().getResponse().getResults());
// populateDb(articles);
// }
// }
//
// @Override
// public void onFailure(Call<Example> call, Throwable t) {
// Log.d(TAG, "onFailure: " + t.getStackTrace());
// }
// });

//My web service is not available currently, so I use dummy data
populateDb(DummyData.populateData());
}

public LiveData<List<Article>> getArticles() {
LiveData<List<Article>> articles = mArticleDao.getAllArticles();
return articles;
}

public void populateDb(List<Article> articles) {
Log.d(TAG, "populateDb");
Article[] articleArray = new Article[articles.size()];
for (int i = 0; i < articles.size(); i++) {
articleArray[i] = articles.get(i);
}

new insertAsyncTask(mArticleDao).execute(articleArray);
}


public List<Article> pojoToEntity(List<Result> resultList) {
List<Article> articles = new ArrayList<>();
for (Result result : resultList) {
Article article = new Article();
article.setSectionName(result.getSectionName());
article.setWebTitle(result.getWebTitle());
article.setThumbnail(result.getFields().getThumbnail());
articles.add(article);
}
return articles;
}


private static class insertAsyncTask extends AsyncTask<Article, Void, Void> {

private ArticleDao articleDao;

public insertAsyncTask(ArticleDao articleDao) {
this.articleDao = articleDao;
}

@Override
protected Void doInBackground(Article... articles) {
Log.d(TAG, "doInBackground");
articleDao.insertAll(articles);
return null;
}
}
}

带有getter setter方法的实体,当然我没有复制:

@Parcel(Parcel.Serialization.BEAN)
@Entity(tableName = "articles")
public class Article {

@NonNull
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "uid")
private int uid;

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

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

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

道:

@Dao
public interface ArticleDao {

@Insert
void insertAll(Article... articles);

@Query("DELETE FROM articles")
void deleteAll();

@Query("SELECT * FROM articles")
LiveData<List<Article>> getAllArticles();
}

房间数据库:

@Database(entities = {Article.class}, version = 1, exportSchema = false)
public abstract class ArticleRoomDatabase extends RoomDatabase {

public static String TAG = "ArticleRoomDatabase";

private static ArticleRoomDatabase INSTANCE;

public static ArticleRoomDatabase getInstance(final Context context) {
if (INSTANCE == null) {
synchronized (ArticleRoomDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
ArticleRoomDatabase.class, "photo_database")
.build();
}
}
}
return INSTANCE;
}

public abstract ArticleDao articleDao();
}

据 Logging 显示,从 DB 获取文章失败,列表返回 null,但是,如果插入失败或获取...尝试调试但无法获取任何内容,实际上我无法获取。

最佳答案

首先验证插入是否正常工作。您可以通过下面的 DAO 方法获取所有插入行的列表

在您的 DAO 中添加以下方法

@Insert
fun insertAll(articleList : List<Article>) : List<Long>

此方法将返回 uid (primarykey) 列表。

这样你就可以验证数据是否被插入。

一旦它工作了,让我知道我会帮你解决 getAllData()

关于android - 为什么这个 Android Room 插件不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51029028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com