How to change RealmList to RealmResult?

I am creating a android java application and I am using realm.io for my database. My problem is that I have a RealmList and my ListView custom adapter accepts only RealmResults. Below is the code and more details.

I have a Chat class that has RealmList, RealmList, userId and chatId.

public class Chat extends RealmObject{ private RealmList<Friend> participants; private RealmList<Message> messages; @PrimaryKey private String chatId; private String userId; ... } 

In my work, when I try to display all the messages that are in the chat, I can call chat.getMessages () to get all the messages for this chat as a RealmList, but my ListView adapter below accepts RealmResult because it extends RealmBaseAdapter

 public class MessageAdapter extends RealmBaseAdapter<Message> implements ListAdapter { private String TAG = getClass().getSimpleName(); public MessageAdapter(Context context, RealmResults<Message> realmResults, boolean automaticUpdate) { super(context, realmResults, automaticUpdate); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.listitem_message, parent, false); } Message message = getRealmResults().get(position); if (message != null) { ((TextView) convertView.findViewById(R.id.message_content)).setText(message.getContent()); DateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.CANADA); ((TextView) convertView.findViewById(R.id.message_time)).setText(dateFormat.format(message.getTimestamp())); } return convertView; } public RealmResults<Message> getRealmResults() { return realmResults; } } 

Here i call everything

 RealmList<Message> messages = chat.getMessages(); ListView messageList = (ListView) findViewById(R.id.message_list); adapter = new MessageAdapter(this, messages, true); messageList.setAdapter(adapter); 

I am open to changing my RealmList to RealmResult if it is possible (I looked and it doesn’t look like it), or if I can use RealmList in a realm user adapter, which will be another solution. Anything that helps me move forward will be of great help.

thanks

+6
source share
2 answers

Just do

 chat.getMessages().where().findAll() 

(Answer from here )

+12
source

RealmBaseAdapter has a very simple implementation .

So, in this case, you can pass the specific Chat adapter to you and overload the methods below:

 @Override public int getCount() { if (realmResults == null || realmResults.size() == 0) { return 0; } return realmResults.first().getMessages().size(); } @Override public T getItem(int i) { if (realmResults == null || realmResults.size() == 0) { return null; } return realmResults.first().getMessages().get(i); } @Override public View getView(int position, View convertView, ViewGroup parent) { // You message view here } 
+1
source

All Articles