我正在使用RecyclerView开发简历,如何将翻新api响应保存到领域中并在Android上离线使用?
在实现了CV简介和图像的Adapter下方
公共类IntroductionAdapter扩展了RecyclerView.Adapter {
private List<Introduction> introductionList;
Context context; // changes
public IntroductionAdapter(Context context, List<Introduction> introductionList) { // changes
this.context = context; // changes(here you can see context)
this.introductionList = introductionList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context)
.inflate(R.layout.introduction_list, parent, false); // change
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
final Introduction introduction = introductionList.get(position);
if (introduction.getImage() != null) {
Glide.with(holder.imageView).load(introduction.getImage()).into(holder.imageView);
}
holder.introduction.setText(introduction.getIntroduction());
}
@Override
public int getItemCount() {
return introductionList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView introduction, about;
public CircularImageView imageView;
public MyViewHolder(View view) {
super(view);
introduction = (TextView) view.findViewById(R.id.introduction);
about = (TextView) view.findViewById(R.id.about);
imageView = (CircularImageView) view.findViewById(R.id.circularImageView);
}
}
}
在IntroductionItem.java下面,我在其中以离线模式实现了Realm
公共类IntroductionItem扩展了AppCompatActivity { 公共RealmList IntroductionList;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.introduction);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
//TODO move this initialization to App extends Application
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().build();
final Realm realm = Realm.getInstance(realmConfiguration);
KitabInterface kitabInterface = ApiClient.getApiService();
Call<KitabSawti> call = kitabInterface.getIntroduction();
call.enqueue(new Callback<KitabSawti>() {
@Override
public void onResponse(Call<KitabSawti> call, Response<KitabSawti> response) {
introductionList = response.body().getIntroduction();
recyclerView.setAdapter(new IntroductionAdapter(IntroductionItem.this, introductionList));
realm.beginTransaction();
for (int i = 0; i < introductionList.size(); i++) {
Introduction introduction = realm.createObject(Introduction.class);
introduction.setImage(introductionList.get(i).getImage());
introduction.setIntroduction(introductionList.get(i).getIntroduction());
}
realm.commitTransaction();
}
@Override
public void onFailure(Call<KitabSawti> call, Throwable t) {
List<Introduction> list = realm.where(Introduction.class).findAll();
if (list != null) {
recyclerView.setAdapter(new IntroductionAdapter(IntroductionItem.this, list));
}
}
});
}
}
在Introduction.java类下面 公共类简介扩展了RealmObject {
@SerializedName("image")
@Expose
private String image;
@SerializedName("introduction")
@Expose
private String introduction;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
}
答案 0 :(得分:0)
我相信每次获得成功响应时,您只会在域中添加相同的结果,这说明了为什么在离线模式下重复出现项目。
如果只想覆盖现有结果,可以尝试:
@Override
public void onResponse(Call<KitabSawti> call, Response<KitabSawti> response) {
introductionList = response.body().getIntroduction();
recyclerView.setAdapter(new IntroductionAdapter(IntroductionItem.this, introductionList));
realm.beginTransaction();
realm.delete(Introduction.class); // Remove older values first
realm.commitTransaction();
realm.beginTransaction();
for (int i = 0; i < introductionList.size(); i++) {
Introduction introduction = realm.createObject(Introduction.class);
introduction.setImage(introductionList.get(i).getImage());
introduction.setIntroduction(introductionList.get(i).getIntroduction());
}
realm.commitTransaction();
}
如果您使用的是旧版Realm,则为realm.clear(Introduction.class)
。为了使该解决方案有效,您可能需要清除应用程序数据,或者直接卸载并重新安装。
否则,您可能需要找到其他解决方法,以便您的Introduction
类支持唯一属性。
答案 1 :(得分:0)
对此不确定。但是您可以尝试在适配器中添加这两项:
@Override
public long getItemId(int position) {
return position;
}
并像这样更改您的构造函数:
public IntroductionAdapter(Context context, List<Introduction> introductionList) {
this.context = context;
this.introductionList = introductionList;
setHasStableIds(true);
}