7

I am building a Java android app and I am using realm.io for my database. My problem is I have a RealmList and my Custom ListView adapter only accepts RealmResults. Below is the code and more details.

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

public class Chat extends RealmObject{

private RealmList<Friend> participants;
private RealmList<Message> messages;

@PrimaryKey
private String chatId;
private String userId;

...
}

In my activity where I am trying to display all the messages that the chat has, I can call chat.getMessages() to get all the messages for this chat as a RealmList but my ListView adapter below takes a 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 is where I call it all

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 a RealmResult if possible (I have looked and it doesn't seem to be) or If I can use a RealmList in the custom realm adapter that would another solution. Anything to help me move forward would be great help.

thanks

2 Answers 2

15

Just do

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

(Answer from here)

1

RealmBaseAdapter has a very simple implementation.

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

@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
}
9
  • Doing this, does it manage automaticUpdate for object in relation? Oct 14, 2015 at 20:00
  • Yes, it does. When the object in relation changes, the changeListener will be triggered as well. And RealmBaseAdapter.notifyDataSetChanged will be called.
    – beeender
    Oct 15, 2015 at 6:55
  • what does T represents ? because i am getting incompatible return type error
    – Hunt
    Apr 26, 2016 at 13:41
  • @Hunt See github.com/realm/realm-java/blob/master/realm/realm-library/src/… for the T
    – beeender
    Apr 26, 2016 at 13:48
  • ok , the issue i am facing is my adapter class is as public class CompensationProductAdapter extends RealmBaseAdapter<CompensationList> and when i am overriding getItem by using @Override public CompensationProductsList getItem(int i) { if (compensationList == null || compensationList.size() == 0) { return null; } return compensationList.first().getCompensationProductsLists().get(i); } i am getting incompatible error
    – Hunt
    Apr 26, 2016 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.