gpt4 book ai didi

android - 房间插入POJO列表并在Recyclerview中检索

转载 作者:行者123 更新时间:2023-11-29 00:55:28 24 4
gpt4 key购买 nike

我正在使用房间作为我的应用程序的数据存储。我正在尝试从 viewmodel 类中的成功网络调用中保存 session 列表。我使用了一个存储库来与 dao 交互,并使用 asynctask 来使 crud 操作异步。

现在,我试图在回收站 View 中显示“保存的数据”,但它什么也没显示。在检查我的数据库表时,我发现没有任何内容被保存。这是我的模型类:

@Entity(tableName = "sessions")
public class Sessions{

// @PrimaryKey(autoGenerate = true)
// public int id;

@SerializedName("prg_session_image")
public String sessionImage;

@SerializedName("prg_session_name")
public String session_name;

@SerializedName("prg_session_id") // used session id as PK
@PrimaryKey
@NonNull
public String prog_sessionId;

@SerializedName("prg_session_description")
public String session_desc;

@SerializedName("reference_id")
public String reference_id;

@SerializedName("prg_name")
public String program_name;

@SerializedName("batch_name")
public String batch_name;

@SerializedName("player_count")
public String participants_count;

@SerializedName("prg_session_focus_points")
public String session_focus_points;

@SerializedName("prg_session_equipment")
public String equipments_reqd;

@SerializedName("session_complete")
public String is_complete;

public Sessions() {
}

// public int getId() {
// return id;
// }

public String getSessionImage() {
return sessionImage;
}

public void setSessionImage(String sessionImage) {
this.sessionImage = sessionImage;
}

public String getSession_name() {
return session_name;
}

public void setSession_name(String session_name) {
this.session_name = session_name;
}

public String getProg_sessionId() {
return prog_sessionId;
}

public void setProg_sessionId(String prog_sessionId) {
this.prog_sessionId = prog_sessionId;
}

public String getSession_desc() {
return session_desc;
}

public void setSession_desc(String session_desc) {
this.session_desc = session_desc;
}

public String getReference_id() {
return reference_id;
}

public void setReference_id(String reference_id) {
this.reference_id = reference_id;
}

public String getProgram_name() {
return program_name;
}

public void setProgram_name(String program_name) {
this.program_name = program_name;
}

public String getBatch_name() {
return batch_name;
}

public void setBatch_name(String batch_name) {
this.batch_name = batch_name;
}

public String getParticipants_count() {
return participants_count;
}

public void setParticipants_count(String participants_count) {
this.participants_count = participants_count;
}

public String getSession_focus_points() {
return session_focus_points;
}

public void setSession_focus_points(String session_focus_points) {
this.session_focus_points = session_focus_points;
}

public String getEquipments_reqd() {
return equipments_reqd;
}

public void setEquipments_reqd(String equipments_reqd) {
this.equipments_reqd = equipments_reqd;
}

public String getIs_complete() {
return is_complete;
}

public void setIs_complete(String is_complete) {
this.is_complete = is_complete;
}
}

和 Dao 类:

    @Dao
public interface SessionsDAO {

// @Insert
// LiveData<List<Sessions>> saveSessions(List<Sessions> sessions);

@Insert
void addSessions(List<Sessions> list);

@Query("select * from sessions")
LiveData<List<Sessions>> getAllSessions();

@Query("select * from sessions where prog_sessionId = :id")
Sessions getSessionById(String id);

}

在存储库中,我有针对 Dao 的各种操作的异步任务:

    public class SessionsRepository {

public SessionsDAO dao;
private MutableLiveData<List<Sessions>> querySingleSession;
private LiveData<List<Sessions>> allSessions;

public SessionsRepository(Application application){
SportsDatabase database = SportsDatabase.getInstance(application);
dao = database.sessionsDAO();
querySingleSession = new MutableLiveData<>();
allSessions = dao.getAllSessions();
}

public void saveSessions(List<Sessions> sessions){
new SaveSessionsTask(dao).execute(sessions);
}

public LiveData<List<Sessions>> getAllSessions() {
return allSessions;
}

public void getSessionById(List<Sessions> sessions){
querySingleSession.setValue(sessions);
}

public class SaveSessionsTask extends AsyncTask<List<Sessions>, Void, Void>{
private SessionsDAO dao;

public SaveSessionsTask(SessionsDAO dao) {
this.dao = dao;
}

@Override
protected Void doInBackground(List<Sessions>... lists) {
dao.addSessions(lists[0]);
return null;
}
}

// public void getSessions(){
// new GetSessionsTask(dao).execute();
// }
// public class GetSessionsTask extends AsyncTask<Void, >

}

我现在正在尝试保存网络调用的所有结果并从数据库中显示它们。这是我在 viewmodel 类中的操作:

    public class HomeSessionsViewModel extends AndroidViewModel {

private static final String TAG = HomeSessionsViewModel.class.getSimpleName();

private MutableLiveData<SessionDetails> liveDetails;
private SessionsRepository repository;

public HomeSessionsViewModel(@NonNull Application application) {
super(application);
repository = new SessionsRepository(application);
}

// public HomeSessionsViewModel (Application application){
// repository = new SessionsRepository(application);
// }

public MutableLiveData<SessionDetails> getSessions(){
if (liveDetails == null){
liveDetails = new MutableLiveData<>();
fetchSessions();
}
return liveDetails;
}

private void fetchSessions(){
String coachId = "4086";

Call<SessionDetails> call = RestClient.getRestInstance().getSessionsService().fetchSessions(coachId);

call.enqueue(new Callback<SessionDetails>() {
@Override
public void onResponse(Call<SessionDetails> call, Response<SessionDetails> response) {
if (response.isSuccessful()){
SessionDetails details = response.body();
List<Sessions> sessions = details.getSessions();
Log.d(TAG, "N/w sesh size:\t" + sessions.size());
liveDetails.setValue(details); // now just displaying from network
saveSessions(sessions);
}
}

@Override
public void onFailure(Call<SessionDetails> call, Throwable t) {

}
});

}

private void saveSessions(List<Sessions> sessions) {
repository.saveSessions(sessions);
}

public LiveData<List<Sessions>> fetchSessionsDB(){
return repository.getAllSessions();
}

}

在 ui Controller ( fragment )中,我调用了 View 模型的 fetchSessionsDB() 方法,但未显示任何数据。网络请求运行良好,就像我在添加房间之前从那里显示的那样。这里有什么问题?谢谢。

API 响应:

{
"session_details": [
{
"prg_session_name": "Session-16",
"prg_session_id": "987",
"prg_session_equipment": null,
"prg_session_description": "",
"prg_session_focus_points": "",
"prg_session_image": "http://devsports.copycon.in/includes/uploads/Jellyfish5.jpg",
"session_complete": "0",
"prg_name": "cricket coaching",
"reference_id": "293",
"batch_id": "57",
"batch_name": "Batch 3",
"player_count": "10"
}, .... ]}

和 SessionDetails POJO:

public class SessionDetails {

@SerializedName("session_details")
@Expose
private List<Sessions> sessions;

@SerializedName("status")
private String status;

@SerializedName("message")
private String msg;

public List<Sessions> getSessions() {
return sessions;
}
}

应显示数据库数据的 fragment 类:

private void populateSessions() {
sessionsRV = fragmentBinding.sessionsRV;
sessionsRV.setHasFixedSize(false);
LinearLayoutManager hlm = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
sessionsRV.setLayoutManager(hlm);

sessionsViewModel = ViewModelProviders.of(this).get(HomeSessionsViewModel.class);
// sessionsViewModel.fetchSessions(""); // TODO: 3/16/2019 Use coach id from db

// calling db from viewmodel
sessionsViewModel.fetchSessionsDB().observe(this, new Observer<List<Sessions>>() {
@Override
public void onChanged(@Nullable List<Sessions> sessions) {
sessionsAdapter = new SessionsAdapter(getActivity(), sessions);
sessionsRV.setAdapter(sessionsAdapter);
Log.d(TAG, "Sessions Count:\t" + sessionsAdapter.getItemCount()); // logs 0
}
});

// previously from network directly displayed
// sessionsViewModel.getSessions().observe(this, new Observer<SessionDetails>() {
// @Override
// public void onChanged(@Nullable SessionDetails details) {
// List<Sessions> list = details.getSessions();
// sessionsAdapter = new SessionsAdapter(getActivity(), list);
// sessionsRV.setAdapter(sessionsAdapter);
// Log.d(TAG, "Sessions Count:\t" + sessionsAdapter.getItemCount());
// }
// });

}

体育数据库类:

@Database(entities = {CoachDB.class, Sessions.class}, version = 1, exportSchema = false)
public abstract class SportsDatabase extends RoomDatabase {

private static SportsDatabase instance;
public abstract CoachDAO coachDAO();
public abstract SessionsDAO sessionsDAO();

public static synchronized SportsDatabase getInstance(Context context) {
if (instance == null){
instance = Room.databaseBuilder(context.getApplicationContext(), SportsDatabase.class, "sports_db")
.fallbackToDestructiveMigration()
.build();
}
return instance;
}

}

最佳答案

我已经通过修改 dao 中的 @insert 方法解决了这个问题

@Dao
public interface SessionsDAO {

@Insert
void addSessions(List<Sessions> sessions);

@Query("select * from sessions")
LiveData<List<Sessions>> getAllSessions();

@Query("select * from sessions where prog_sessionId = :id")
Sessions getSessionById(String id);

}

并以 session 列表作为输入运行我的异步任务,它成功运行。

private void saveSessions(List<Sessions> sessions) {
new SaveSessionsTask(dao).execute(sessions);
}

public class SaveSessionsTask extends AsyncTask<List<Sessions>, Void, Void> {
private SessionsDAO dao;

public SaveSessionsTask(SessionsDAO dao) {
this.dao = dao;
}

@Override
protected Void doInBackground(List<Sessions>... lists) {
dao.addSessions(lists[0]);
return null;
}
}

关于android - 房间插入POJO列表并在Recyclerview中检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55247462/

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