Snowdinjon Edgarjon
Snowdinjon Edgarjon

Reputation: 83

how to save retrofit api response into realm and use offline on android?

I am developing cv using RecyclerView how can I save retrofit api response into realm and use offline on android?

below my Adapter where I have implemented CV introduction and image

public class IntroductionAdapter extends 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);

    }
}

}

below IntroductionItem.java where I have implemented Realm in offline mode

public class IntroductionItem extends AppCompatActivity { public 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));

            }

        }

    });


}

}

below Introduction.java class public class Introduction extends 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;
}

}

Upvotes: 0

Views: 962

Answers (2)

Sifat Ullah Chowdhury
Sifat Ullah Chowdhury

Reputation: 81

Not sure about this. But you can try adding this two things in your adapter:

@Override
public long getItemId(int position) {
    return position;
}

And change your constructor like this:

public IntroductionAdapter(Context context, List<Introduction> introductionList) {   
    this.context = context;    
    this.introductionList = introductionList;
    setHasStableIds(true);

}

Upvotes: 0

Aaron
Aaron

Reputation: 3894

I believe every time when you get a success response, you are only adding the same result to the realm, which explains why you're getting repeating items when in offline mode.

If you only want to overwrite existing results, you could try:

    @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();
    }

Or realm.clear(Introduction.class) if you're using older Realm. And for this solution to work, you may need to clear your app data, or simply uninstall and install again.

Otherwise you may need to find another workaround so that your Introduction class support unique property.

Upvotes: 0

Related Questions