在CursorAdapter中滚动会导致崩溃应用程序

时间:2011-09-15 09:39:00

标签: android

我需要获取已点击的项目的ID。所以在bindView中我通过holder.layout.setTag(id)将id设置为viewHolder,在onClick中我通过view.getTag获取此值。当我运行它时,这段代码工作得很好但是当我开始在几个项目应用程序崩溃时滚动它时,在日志中我可以在bindView中看到ClassCastException javaLanString。所以,没有那些setTag和getTag代码完美的工作。 几个小时后,我试着找出为什么我得到ClassCastException,但我找不到答案。你有想法吗?

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();
    String value = cursor.getString(itemId);
    Log.i("MINE", value);
    holder.layout.setTag(value);
    holder.name.setText(cursor.getString(itemName));
    holder.country.setText(cursor.getString(itemCountry));

 @Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View view = inflater.inflate(com.home.R.layout.item_list_row,
            parent, false); 
    ViewHolder holder = new ViewHolder(
            (TextView) view.findViewById(com.home.R.id.item_row_name),
            (TextView) view.findViewById(com.home.R.id.item_row_population),
            (TextView) view.findViewById(com.home.R.id.item_row_country), 
            (ImageView) view.findViewById(com.home.R.id.icon), 
            (View) view.findViewById(R.id.row_item));
    view.setTag(holder);
    view.setOnClickListener(this);
    return view;
}

 private static final class ViewHolder {
    final TextView name;
    final TextView population;
    final TextView country;
    final ImageView icon;
    final View layout;

    public ViewHolder(TextView name, TextView population, TextView country,
            ImageView icon, View layout) {
        this.name = name;
        this.population = population;
        this.country = country;
        this.icon = icon;
        this.layout = layout;
    }
}

编辑:我在此行中收到错误ViewHolder holder =(ViewHolder)view.getTag();下面我把stacktrace显示出错误发生的地方     09-15 13:35:21.586:ERROR / AndroidRuntime(2175):致命异常:主要 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):java.lang.ClassCastException:java.lang.String 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):at com.dataart.representation.ItemsAdapter.bindView(ItemsAdapter.java:38) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.widget.CursorAdapter.getView(CursorAdapter.java:186) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.widget.AbsListView.obtainView(AbsListView.java:1397) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.widget.ListView.addViewBelow(ListView.java:2914) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.widget.ListView.scrollListItemsBy(ListView.java:2843) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.widget.ListView.arrowScrollImpl(ListView.java:2340) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.widget.ListView.arrowScroll(ListView.java:2287) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.widget.ListView.commonKey(ListView.java:2089) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.widget.ListView.onKeyDown(ListView.java:2036) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.view.KeyEvent.dispatch(KeyEvent.java:1256) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.view.View.dispatchKeyEvent(View.java:3855) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:787) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.widget.ListView.dispatchKeyEvent(ListView.java:2021) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):at com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchKeyEvent(PhoneWindow.java:1687) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1120) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.app.Activity.dispatchKeyEvent(Activity.java:2073) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):at com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchKeyEvent(PhoneWindow.java:1663) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.view.ViewRoot.handleMessage(ViewRoot.java:1867) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.os.Handler.dispatchMessage(Handler.java:99) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.os.Looper.loop(Looper.java:123) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):在android.app.ActivityThread.main(ActivityThread.java:3647) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):at java.lang.reflect.Method.invokeNative(Native Method) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):at java.lang.reflect.Method.invoke(Method.java:507) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 09-15 13:35:21.586:ERROR / AndroidRuntime(2175):at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您使用setTag()致电String,并使用setTag()覆盖您之前执行的ViewHolder

您无需使用setTag()致电String。您可以通过致电_ID上的getItemId()随时获取CursorAdapter值。

如果您发现该答案不可接受,请将long id数据成员添加到ViewHolder并将_ID放在那里。