0

I want to call the method getTime of my pojo class from my activity , but i want to use the adapter's position because i need the users time for the message object which is at the topmost position of a recyclerview this is the code i am trying

((ConvoResponseModel.DataBean.ConversationBean) adapter.getItemId(0)).getTime();

but i am getting inconvertible types long cannot be cast to ConversationBean error , what is the correct way to achieve this ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

1

use getItem()instead of getItemId() see https://developer.android.com/reference/android/widget/ArrayAdapter.html#getItem(int)

Fusselchen
  • 382
  • 1
  • 4
  • 12
0

You're trying to call the method getTime() from a long value. The getItemId() won't return the object you want, it'll return, as the method signature suggests, only the ID (of the type long).

I don't know Adapter subclass you're using, but most likely you have to implement a method to retrieve the item on the provided position. Something like:

private MyItem getItemAtPosition(int position) {
    return mItems.get(position);
}

Edit: Also, you should read the docs on BaseAdapter and this tutorial on how to subclass BaseAdapter to fill a custom view.

If you're using an ArrayAdapter, check Fusselchen answer.

I recommend that you see this related question on how to interpret the stack trace. That can be really helpful while debugging. On your particular case it would've lead to the conversion problem.

P.S.: You should try Udacity's Java and Android courses, or any other material on Java and Android. They will give you more experience when developing apps and will help you to solve those kinds of problems in the future.

Google is a powerful tool, use it to your advantage.

JAAD
  • 12,349
  • 7
  • 36
  • 57
Mauker
  • 11,237
  • 7
  • 58
  • 76