Kevin D
Kevin D

Reputation: 29

Hashmap Adapter doesn't like the Textview cast for displaying

So I was making a ArrayAdapter which takes a HashMap(String, Card) where Card is just a custom class of two strings basically to display information on a ListView later So I rewrite the getView method like:

public View getView(int position, View changeView, ViewGroup parent)
{
    final View result;
    if(changeView == null)
    {
        result = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_adapter_item, parent,false);
    }
    else
        result = changeView;
    Map.Entry<String, String> item = getItem(position);

    //replace findViewById by ViewHolder
     ((TextView) result.findViewById(android.R.id.text1)).setText(item.getKey());
Error here--> ((TextView) result.findViewById(android.R.id.text2)).setText(item.getValue());
    return result;
}

And I get the Error Card cannot be cast to java.lang.CharSequence

Is there maybe a better way to display the info rather than just casting it as a textview?

Thanks!

Here is the XML code to the my_adapter_item

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView 
    android:id = "@android:id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
<TextView 
    android:id = "@android:id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

Here is the log cat too

05-25 10:43:11.929: D/AndroidRuntime(687): Shutting down VM
05-25 10:43:11.929: W/dalvikvm(687): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
05-25 10:43:12.069: E/AndroidRuntime(687): FATAL EXCEPTION: main
05-25 10:43:12.069: E/AndroidRuntime(687): java.lang.ClassCastException: com.example.cthulumanager.Card cannot be cast to java.lang.CharSequence
05-25 10:43:12.069: E/AndroidRuntime(687):  at com.example.cthulumanager.HashArrayAdapter.getView(HashArrayAdapter.java:46)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.widget.AbsListView.obtainView(AbsListView.java:2271)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.widget.ListView.onMeasure(ListView.java:1156)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.View.measure(View.java:15172)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.View.measure(View.java:15172)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.View.measure(View.java:15172)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.View.measure(View.java:15172)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-25 10:43:12.069: E/AndroidRuntime(687):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.View.measure(View.java:15172)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.Choreographer.doFrame(Choreographer.java:525)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.os.Handler.handleCallback(Handler.java:615)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.os.Looper.loop(Looper.java:137)
05-25 10:43:12.069: E/AndroidRuntime(687):  at android.app.ActivityThread.main(ActivityThread.java:4745)
05-25 10:43:12.069: E/AndroidRuntime(687):  at java.lang.reflect.Method.invokeNative(Native Method)
05-25 10:43:12.069: E/AndroidRuntime(687):  at java.lang.reflect.Method.invoke(Method.java:511)
05-25 10:43:12.069: E/AndroidRuntime(687):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-25 10:43:12.069: E/AndroidRuntime(687):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-25 10:43:12.069: E/AndroidRuntime(687):  at     dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 39

Answers (1)

Blackbelt
Blackbelt

Reputation: 157457

Your HashMap is of the type <String, Card>, so you can't simply cast the returned value of getValue() to CharSequence, but you have to assign the return value of getValue() to an object of type Card and then access is properties. Supposing the Card contains a public String mValue, you can access mValue through cardInstance.mValue. E.g.

public View getView(int position, View changeView, ViewGroup parent)
{
    final View result;
    if(changeView == null)
    {
        result = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_adapter_item, parent,false);
    }
    else
        result = changeView;
    Map.Entry<String, Card> item = getItem(position);
    Card card = item.getValue();
     ((TextView) result.findViewById(android.R.id.text1)).setText(item.getKey());
    ((TextView) result.findViewById(android.R.id.text2)).setText(card.mValue);
    return result;
}

Upvotes: 1

Related Questions