MVVM with Hilt,RxJava 3,Retrofit, Live Data and View Binding - java

I currently get stock on this part. I want to display simple data from GSON and I practice using MVVM structure and with Hilt and "RxJava".
This is my "View Model" and I don't know what to do.(This is my first time using this structure and dependencies.)
At my "getTip()" function under my View Model how can i transfer the data from "tipsModel" to arraylist and if i did something wrong at my codes.
public class TipsViewModel extends ViewModel {
private static final String TAG = "TipsViewModel";
private Repository repository;
private MutableLiveData<ArrayList<TipsModel>> tipsList = new MutableLiveData<>();
#ViewModelInject
public TipsViewModel(Repository repository){
this.repository = repository;
}
public MutableLiveData<ArrayList<TipsModel>> getTipsList(){
return tipsList;
}
public void getTips(){
repository.getTips()
.subscribeOn(Schedulers.io())
.map(new Function<TipsModel, ArrayList<TipsModel>>() {
#Override
public ArrayList<TipsModel> apply(TipsModel tipsModel) throws Throwable {
// What i have to put here to transfer the data from "tipsModel" to my ArrayList.
return null;
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> tipsList.setValue(result),
error -> Log.e(TAG,"getTips: "+error.getMessage()));
}
}
This is my Repository
public class Repository {
private TipsApiService apiService;
#Inject
public Repository(TipsApiService apiService){
this.apiService = apiService;
}
public Observable<TipsModel> getTips(){
return apiService.getTips();
}
}
And this is my Model. it a simple model.
public class TipsModel {
private Integer id;
private String gameName;
private String gameMenu;
private String subtitle;
private String description;
private Object image;
public TipsModel(Integer id, String gameName, String gameMenu, String subtitle, String description, Object image) {
this.id = id;
this.gameName = gameName;
this.gameMenu = gameMenu;
this.subtitle = subtitle;
this.description = description;
this.image = image;
}
public Integer getId() {
return id;
}
public String getGameName() {
return gameName;
}
public String getGameMenu() {
return gameMenu;
}
public String getSubtitle() {
return subtitle;
}
public String getDescription() {
return description;
}
public Object getImage() {
return image;
}
public void setId(Integer id) {
this.id = id;
}
public void setGameName(String gameName) {
this.gameName = gameName;
}
public void setGameMenu(String gameMenu) {
this.gameMenu = gameMenu;
}
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
public void setDescription(String description) {
this.description = description;
}
public void setImage(Object image) {
this.image = image;
}
}
This is my Code on my fragment
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
binding = ActivityTipsViewBinding.inflate(inflater,container,false);
return binding.getRoot();
}
#Override
public void onViewCreated(#Nonnull View view, #Nullable Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
viewModel = new ViewModelProvider(this).get(TipsViewModel.class);
initRecyclerView();
observeData();
viewModel.getTips();
}
private void observeData(){
viewModel.getTipsList().observe(getViewLifecycleOwner(), new Observer<ArrayList<TipsModel>>() {
#Override
public void onChanged(ArrayList<TipsModel> tipsModels) {
Log.e(TAG,"onChanged: "+ tipsModels.size());
tipsAdapter.updateList(tipsModels);
}
});
}
private void initRecyclerView(){
binding.tipsRecyclerview.setLayoutManager(new LinearLayoutManager((getContext())));
tipsAdapter = new TipsAdapter(getContext(),tipsModels);
binding.tipsRecyclerview.setAdapter(tipsAdapter);
}
}

Related

incompatible types: cant compare a class to a native ad

I am new to android studios I am currently changing my FirestoreRecyclerOptions to a regular recycler view because I want to add native ads every 5 posts. The current issue I am facing is that the method getItemViewType what I want it to return is if it's either an ad or regular post using instance of. In the tutorial videos they do something along the line of
#Override
public int getItemViewType(int position) {
if (noteList.get(position) instanceof UnifiedNativeAd) {
return TYPE_AD;
}else {
return TYPE_REG;
}
}
But the condition inside the if statement is giving me this error
error: incompatible types: Note cannot be converted to UnifiedNativeAd
if (noteList.get(position) instanceof UnifiedNativeAd) {
CLASS
public class Note {
public int timestamp;
public List<String> replies;
public String ownerName;
public String ownerId;
public String text;
public String imageURL;
public List<String> usersLiked;
public String message;
public String timePosted;
public int likes;
public int replyCount;
public Note() {
}
public Note(int timestamp, String ownerId, String text, String ownerName, String imageURL, List<String> replies, List<String>
usersLiked, String message, String timePosted, int likes, int replyCount) {
this.timestamp = timestamp;
this.ownerId = ownerId;
this.text = text;
this.ownerName = ownerName;
this.imageURL = imageURL;
this.replies = replies;
this.usersLiked = usersLiked;
this.message = message;
this.timePosted = timePosted;
this.likes = likes;
this.replyCount = replyCount;
}
public int getTime() {
return timestamp;
}
public String getOwnerName() {
return ownerName;
}
public String getId() {
return ownerId;
}
public String getPost() {
return text;
}
public List<String> getreplies() {
return replies;
}
public String getImageURL() {
return imageURL;
}
public List<String> getUsersLiked() {
return usersLiked;
}
public String getMessage() {
return message;
}
public String getTimePosted() {
return timePosted;
}
public int getLikes() {
return likes;
}
public int getReplyCount() {
return replyCount;
}
}
FULL RECYCLERVIEW
ublic class adapterRegular extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final static int TYPE_AD=0;
private final static int TYPE_REG=1;
private Context context;
private List<Note> noteList;
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
if(viewType == TYPE_AD){
View view = LayoutInflater.from(context).inflate(R.layout.reclyerads,parent,false);
return new AdTemplateViewHolder(view);
}
View view = LayoutInflater.from(context).inflate(R.layout.list_row,parent,false);
return new noteHolder(view);
}
#Override
public int getItemCount() {
return noteList.size();
}
public class noteHolder extends RecyclerView.ViewHolder{
TextView ownerName;
TextView timestamp;
TextView text;
ImageView imageURL;
TextView replies;
TextView likes;
ImageView heart;
public noteHolder(#NonNull View itemView) {
super(itemView);
ownerName = itemView.findViewById(R.id.userName);
timestamp = itemView.findViewById(R.id.time);
text = itemView.findViewById(R.id.post);
imageURL = itemView.findViewById(R.id.profilePic);
replies = itemView.findViewById(R.id.REPLY);
likes = itemView.findViewById(R.id.likes);
heart = itemView.findViewById(R.id.heart);
}
}
public class AdTemplateViewHolder extends RecyclerView.ViewHolder{
TemplateView templateView;
public AdTemplateViewHolder(#NonNull View itemView) {
super(itemView);
templateView = itemView.findViewById(R.id.my_template);
NativeTemplateStyle style = new NativeTemplateStyle.Builder()
.withMainBackgroundColor(new ColorDrawable(Color.parseColor("#FFFFFF"))).build();
templateView.setStyles(style);
}
public void setUnifiedNativeAd(UnifiedNativeAd ads){
templateView.setNativeAd(ads);
}
}
#Override
public int getItemViewType(int position) {
if (noteList.get(position) instanceof UnifiedNativeAd) {
return TYPE_AD;
}else {
return TYPE_REG;
}
}
}
ANY HELP OR SUGGESTION IS APPERCIATED
Change this
private List<Note> noteList;
To this
private List<Object> noteList;

Receiving null Parcelable object Android

I'm trying to pass an object from my RecyclerView adapter to a fragment using parcelable. However when I try to receive the data, the bundle is null. I've looked at other examples, but I can't see where I'm going wrong.
Parcelable class
public class Country extends BaseObservable implements Parcelable {
#SerializedName("name")
#Expose
private String name;
#SerializedName("snippet")
#Expose
private String snippet;
#SerializedName("country_id")
#Expose
private String countryId;
#SerializedName("id")
#Expose
private String id;
#SerializedName("coordinates")
#Expose
private Coordinates coordinates;
#SerializedName("images")
#Expose
private List<CountryImage> images;
protected Country(Parcel in) {
name = in.readString();
snippet = in.readString();
}
public static final Creator<Country> CREATOR = new Creator<Country>() {
#Override
public Country createFromParcel(Parcel in) {
return new Country(in);
}
#Override
public Country[] newArray(int size) {
return new Country[size];
}
};
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(snippet);
}
#Override
public int describeContents() {
return 0;
}
#Bindable
public String getCountryId() {
return countryId;
}
public void setCountryId(String countryId) {
this.countryId = countryId;
notifyPropertyChanged(BR.countryId);
}
#Bindable
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
notifyPropertyChanged(BR.id);
}
#Bindable
public Coordinates getCoordinates() {
return coordinates;
}
public void setCoordinates(Coordinates coordinates) {
this.coordinates = coordinates;
notifyPropertyChanged(BR.coordinates);
}
#Bindable
public List<CountryImage> getImages() {
return images;
}
public void setImages(List<CountryImage> images) {
this.images = images;
notifyPropertyChanged(BR.images);
}
#Bindable
public String getSnippet() {
return snippet;
}
public void setSnippet(String snippet) {
this.snippet = snippet;
notifyPropertyChanged(BR.snippet);
}
#Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyPropertyChanged(BR.name);
}
}
RetrofitAdapter.java
public class RetrofitAdapter extends RecyclerView.Adapter<RetrofitAdapter.MyViewHolder> implements CustomClickListener {
private List<Country> cities;
private CustomClickListener customClickListener;
private View view;
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
RetroItemBinding retroItemBinding =
DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()),
R.layout.retro_item, viewGroup, false);
view = viewGroup;
return new MyViewHolder(retroItemBinding);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.bindTo(cities.get(i));
myViewHolder.retroItemBinding.setItemClickListener(this);
}
#Override
public int getItemCount() {
if (cities != null) {
return cities.size();
} else {
return 0;
}
}
public void setCityList(ArrayList<Country> cities) {
this.cities = cities;
notifyDataSetChanged();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private RetroItemBinding retroItemBinding;
public MyViewHolder(#NonNull RetroItemBinding retroItemBinding) {
super(retroItemBinding.getRoot());
this.retroItemBinding = retroItemBinding;
}
void bindTo(Country country) {
retroItemBinding.setVariable(BR.city, country);
retroItemBinding.setVariable(BR.itemClickListener, customClickListener);
retroItemBinding.executePendingBindings();
}
}
public void cardClicked(Country country) {
CountryFragment countryFragment = new CountryFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("Country", country);
countryFragment.setArguments(bundle);
((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction()
.replace(R.id.frag_container, new CountryFragment())
.commit();
}
}
Where I receive attempt to receive the data in CountryFragment.java
Country country;
Bundle bundle = this.getArguments();
if (bundle != null) {
country = bundle.getParcelable("Country");
}
.replace(R.id.frag_container, new CountryFragment())
should be
.replace(R.id.frag_container, countryFragment)
You are creating a second instance instead of passing the one you set the arguments on.

How do I store some background information associated with each item in a reyclerview in Android using Java?

I am building a social networking app using Cloud Firestore. My Firestore collection posts contains the following fields: title, text, uid, timestamp, latitude, longitude, photo url.
Now when I fetch these posts and display them on a RecyclerView. I do not want the uid, latitude, longitude to be visible on the UI but they should be associated with the item for further use when the item is clicked.
What is the proper way to make the given association?
This is my POJO class:
public class Post {
private String title;
private String dp;
private String description;
private double latitutude,longitude;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
private String uid;
public double getLatitutude() {
return latitutude;
}
public void setLatitutude(double latitutude) {
this.latitutude = latitutude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String getDp() {
return dp;
}
public void setDp(String dp) {
this.dp = dp;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
This is the holder class:
public class PostHolder extends RecyclerView.ViewHolder {
ImageView dp;
TextView title,desc;
public PostHolder(#NonNull View itemView) {
super(itemView);
this.dp = itemView.findViewById(R.id.dpIV);
this.title = itemView.findViewById(R.id.titleTV);
this.desc = itemView.findViewById(R.id.descTV);
}
}
And this is the adapter:
public class PostAdapter extends RecyclerView.Adapter<PostHolder> {
Context c;
ArrayList<Post> posts;
public PostAdapter(Context c, ArrayList<Post> posts) {
this.c = c;
this.posts = posts;
}
#NonNull
#Override
public PostHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row,null);
return new PostHolder(view);
}
#Override
public void onBindViewHolder(#NonNull PostHolder holder, int position) {
holder.title.setText(posts.get(position).getTitle());
holder.desc.setText(posts.get(position).getDescription());
//loadBitmap(position,holder.dp);
Glide.with(holder.dp.getContext())
.load(posts.get(position).getDp())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.apply(RequestOptions.circleCropTransform())
.into(holder.dp);
}
#Override
public int getItemCount() {
Log.e("AdapterSIZE",posts.size()+"");
return posts.size();
}
}

picasso android image not loading

I am trying to get images from an API, and I have written code but images are not loading in emulator
Main activity
public class MainActivity extends ListActivity {
List<Flower> flowerList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RestAdapter restadapter = new RestAdapter.Builder().setEndpoint("http://services.hanselandpetal.com").build();
api flowerapi = restadapter.create(api.class);
flowerapi.getData(new Callback<List<Flower>>() {
#Override
public void success(List<Flower> flowers, Response response) {
flowerList = flowers;
adapter adapt = new adapter(getApplicationContext(),R.layout.item_file,flowerList);
setListAdapter(adapt);
}
#Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(),"Failed",Toast.LENGTH_SHORT).show();
}
});
}
In package model I have flower class
public class Flower {
private int productId;
private String name;
private String category;
private String instructions;
private double price;
private String photo;
private Bitmap bitmap;
public Flower() {
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getInstructions() {
return instructions;
}
public void setInstructions(String instructions) {
this.instructions = instructions;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
}
In network package I have api java class
public interface api {
#GET("/feeds/flowers.jason")
public void getData(Callback<List<Flower>>response);
}
And then I have adaptor java class
public class adapter extends ArrayAdapter<Flower> {
String url="http://services.hanselandpetal.com/photos/";
private Context context;
private List<Flower> flowerList;
public adapter(Context context, int resource, List<Flower> objects) {
super(context, resource, objects);
this.context = context;
this.flowerList = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_file,parent,false);
Flower flower = flowerList.get(position);
TextView tv = (TextView) view.findViewById(R.id.name);
tv.setText(flower.getName());
ImageView img = (ImageView) view.findViewById(R.id.img);
Picasso.with(getContext()).load(url+flower.getPhoto()).resize(100,100).into(img);
return view;
}
}
I am getting nothing on emulator and it says it failed. Is the fault in emulator.
I have included internet permission in manifest.
Might be blowing up on your #GET annotation param "/feeds/flowers.jason"
try ".json"

Using Retrofit 2.0 + RxJava + Realm + RecyclerView saves no data into DB

I'm pretty new to android and third-party libraries, so I have one problem.
I want to store some data coming from REST API into Realm, then show it into RecyclerView. I have three tabs using the same recyclerView, so it can display three different states.
Here's my classes:
public class RealmContentAdapter extends RealmRecyclerViewAdapter<ContentDataModel, RealmContentAdapter.ViewHolder> {
private Context mContext;
public static class ViewHolder extends RecyclerView.ViewHolder{
private ImageView cardIcon;
private TextView likeCounter, cardHeader, cardAddress, cardDate, cardDays;
public ViewHolder(View itemView) {
super(itemView);
this.cardIcon = (ImageView) itemView.findViewById(R.id.card_icon);
this.likeCounter = (TextView) itemView.findViewById(R.id.like_counter);
this.cardHeader = (TextView) itemView.findViewById(R.id.card_header_text);
this.cardAddress = (TextView) itemView.findViewById(R.id.card_address_text);
this.cardDate = (TextView) itemView.findViewById(R.id.card_date_text);
this.cardDays = (TextView) itemView.findViewById(R.id.card_days_text);
}
}
public RealmContentAdapter (Context context, OrderedRealmCollection<ContentDataModel> data){
super(context, data, true);
mContext = context;
}
#Override
public RealmContentAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(RealmContentAdapter.ViewHolder holder, int position) {
ContentDataModel model = getData().get(position);
holder.likeCounter.setText(String.valueOf(model.getLikesCounter()));
holder.cardHeader.setText(model.getTitle());
holder.cardIcon.setImageResource(R.drawable.nature);
if (model.getGeoAddress() != null){
holder.cardAddress.setText(model.getGeoAddress().getAddress());
} else {
holder.cardAddress.setText("");
}
holder.cardDate.setText(model.getNormalDate());
holder.cardDays.setText(String.valueOf(model.getDays()));
}
public class ContentDataModel extends RealmObject {
#SerializedName("id")
#Expose
private int id;
#SerializedName("user")
#Expose
private User user;
#SerializedName("geo_address")
#Expose
private GeoAddress geoAddress;
#SerializedName("category")
#Expose
private Category category;
#SerializedName("type")
#Expose
private Type type;
#SerializedName("title")
#Expose
private String title;
#SerializedName("body")
#Expose
private String body;
#SerializedName("created_date")
#Expose
private int createdDate;
#SerializedName("start_date")
#Expose
private int startDate;
#SerializedName("state")
#Expose
private State state;
#SerializedName("ticket_id")
#Expose
private String ticketId;
#SerializedName("files")
#Expose
#Ignore
private List<Files> files = new ArrayList<>();
#SerializedName("performers")
#Expose
#Ignore
private List<Performer> performers = new ArrayList<>();
#SerializedName("deadline")
#Expose
private int deadline;
#SerializedName("likes_counter")
#Expose
private int likesCounter;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public GeoAddress getGeoAddress() {
return geoAddress;
}
public void setGeoAddress(GeoAddress geoAddress) {
this.geoAddress = geoAddress;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public int getCreatedDate() {
return createdDate;
}
public void setCreatedDate(int createdDate) {
this.createdDate = createdDate;
}
public int getStartDate() {
return startDate;
}
public void setStartDate(int startDate) {
this.startDate = startDate;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public String getTicketId() {
return ticketId;
}
public void setTicketId(String ticketId) {
this.ticketId = ticketId;
}
public List<Files> getFiles() {
return files;
}
public void setFiles(List<Files> files) {
this.files = files;
}
public List<Performer> getPerformers() {
return performers;
}
public void setPerformers(List<Performer> performers) {
this.performers = performers;
}
public int getDeadline() {
return deadline;
}
public void setDeadline(int deadline) {
this.deadline = deadline;
}
public int getLikesCounter() {
return likesCounter;
}
public void setLikesCounter(int likesCounter) {
this.likesCounter = likesCounter;
}
public String getNormalDate(){
return DateFormatter.getNormalDate(getStartDate());
}
public String getDays(){
return DateFormatter.getGoneDays(getStartDate());
}
I have some other pojo classes, but this one is the main.
And my Recycler Fragment:
public class RecyclerFragment extends Fragment {
private static final String KEY_FILE = "file";
private RealmContentAdapter mAdapter;
private RealmResults<ContentDataModel> mData;
private RecyclerView mRecyclerView;
private Realm mRealm;
public static Fragment newInstance(String file) {
RecyclerFragment fragment = new RecyclerFragment();
Bundle args = new Bundle();
args.putString(KEY_FILE, file);
fragment.setArguments(args);
return fragment;
}
public String getPage() {
return (getArguments().getString(KEY_FILE));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.recycler_fragment_layout, container, false);
mIntent = new Intent(getContext(), CardActivity.class);
mRealm = Realm.getDefaultInstance();
mData = mRealm.where(ContentDataModel.class).findAll();
ApiService apiService = ApiModule.getApiService();
Observable<List<ContentDataModel>> tabOneContent = apiService.loadCards(
getString(R.string.processing_state), 10);
Observable<List<ContentDataModel>> tabTwoContent = apiService.loadCards(
getString(R.string.done_state), 10);
Observable<List<ContentDataModel>> tabThreeContent = apiService.loadCards(
getString(R.string.pending_state), 10);
mRecyclerView = (RecyclerView) view.findViewById(R.id.tab_recycler);
mRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(),
LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(layoutManager);
mAdapter = new RealmContentAdapter(getContext(), mData);
mRecyclerView.setAdapter(mAdapter);
if (getPage().equals(getString(R.string.processing_flag))) {
tabOneContent
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<List<ContentDataModel>>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(List<ContentDataModel> dataSet) {
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealm(dataSet);
realm.commitTransaction();
realm.close();
}
});
} else if (getPage().equals(getString(R.string.done_flag))) {
tabTwoContent
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<List<ContentDataModel>>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(List<ContentDataModel> dataSet) {
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealm(dataSet);
realm.commitTransaction();
realm.close();
}
});
} else if (getPage().equals(getString(R.string.pending_flag))) {
tabThreeContent
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<List<ContentDataModel>>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(List<ContentDataModel> dataSet) {
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealm(dataSet);
realm.commitTransaction();
realm.close();
}
});
}
return view;
}
The problem is when I'm trying to save List dataset to Realm, it doesn't save any. And my RealmContentAdapter is empty.
P.S.: I set up GSON carefully and it ignores RealmObject.class
UPD: So, I've found what was wrong. Realm didn't want to save data, because I've missed to add #PrimaryKey to field id in my ContentDataModel. Thank you for attention.

Categories