gpt4 book ai didi

android - Hashmap Adapter 不喜欢用于显示的 Textview 转换

转载 作者:行者123 更新时间:2023-11-30 02:08:09 25 4
gpt4 key购买 nike

所以我制作了一个 ArrayAdapter,它采用 HashMap(String, Card),其中 Card 只是两个字符串的自定义类,基本上是为了稍后在 ListView 上显示信息所以我重写了 getView 方法,如下所示:

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;
}

我得到错误卡无法转换为 java.lang.CharSequence

是否有更好的方式来显示信息而不是将其转换为 TextView ?

谢谢!

这是 my_adapter_item 的 XML 代码

<?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"
/>

这也是日志猫

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)

最佳答案

你的 HashMap类型为 <String, Card> , 所以你不能简单地转换 getValue() 的返回值至 CharSequence , 但你必须分配 getValue() 的返回值到 Card 类型的对象然后访问是属性。假设卡片包含 public String mValue , 您可以访问 mValue通过cardInstance.mValue .例如

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;
}

关于android - Hashmap Adapter 不喜欢用于显示的 Textview 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30443745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com