2

I just check this SO link to add an object at the top of the adapter.

Here is the code which I found that to add an object to at the top of the adapter:

yourAdapter.insert(object, 0);

But in my case the data type of object is different from the data type of adapter. So I am getting following error:

Wrong 1st argument type. required.......

So I decided to cast the object and then try to insert:

adapter.insert((OneOnOneMessage)messageList, 0);

But no luck was with me and started getting following run time error:

03-08 14:55:43.933 6358-6358/com.dayoptions.chat W/System.err: java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.mics.models.OneOnOneMessage
03-08 14:55:43.933 6358-6358/com.dayoptions.chat W/System.err:     at com.mics.activities.SingleChatActivity$2.onClick(SingleChatActivity.java:283)
03-08 14:55:43.933 6358-6358/com.dayoptions.chat W/System.err:     at android.view.View.performClick(View.java:4448)
03-08 14:55:43.933 6358-6358/com.dayoptions.chat W/System.err:     at android.view.View$PerformClick.run(View.java:18461)
03-08 14:55:43.933 6358-6358/com.dayoptions.chat W/System.err:     at android.os.Handler.handleCallback(Handler.java:733)
03-08 14:55:43.933 6358-6358/com.dayoptions.chat W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-08 14:55:43.933 6358-6358/com.dayoptions.chat W/System.err:     at android.os.Looper.loop(Looper.java:136)
03-08 14:55:43.933 6358-6358/com.dayoptions.chat W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5072)
03-08 14:55:43.933 6358-6358/com.dayoptions.chat W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
03-08 14:55:43.933 6358-6358/com.dayoptions.chat W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
03-08 14:55:43.933 6358-6358/com.dayoptions.chat W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-08 14:55:43.933 6358-6358/com.dayoptions.chat W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
03-08 14:55:43.933 6358-6358/com.dayoptions.chat W/System.err:     at dalvik.system.NativeStart.main(Native Method)

I can add an object into the adapter by using following code:

adapter.addAll(messageList);

Using above line I am not getting any issue regarding 'wrong argument type' or 'casting' but I want to add the object at the top of the listview.

Community
  • 1
  • 1
Faisal Shaikh
  • 3,900
  • 5
  • 40
  • 77

2 Answers2

1

The problem is that you can't cast a list(messageList) to an object of type OneOnOneMessage.

you can cast a single item from the list like this:

adapter.insert((OneOnOneMessage)messageList.get(index), 0);
Mina Wissa
  • 10,923
  • 13
  • 90
  • 158
0

Here is my solution:

Created new List and fetched new messages into this:

messageListLoad = OneOnOneMessage.loadEarlierMessages(sessionData.getId(), buddyId, messageList.get(0).getId(), LOAD_EARLIER_COUNT);

Load new created list into the previous list using following code:

for (int i = messageListLoad.size() - 1; i >= 0; i--) {
   messageList.add(0, messageListLoad.get(i));
}

And at the end tells the adapter we have some new values in the list:

adapter.notifyDataSetChanged()

Faisal Shaikh
  • 3,900
  • 5
  • 40
  • 77