0

我在看google 2009,发现我的代码效率低下,可能会导致内存问题。

这是我的实现

public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder viewHolder;
            if(li == null){
            li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        if(convertView == null){
            convertView = li.inflate(R.layout.book_case_view, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.image = (ImageView)convertView.findViewById(R.id.bookCover);
            viewHolder.textView = (TextView)convertView.findViewById(R.id.bookLabel);

            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        bi = dbh.getData(FILES.get(position));

        convertView.setTag(position);
        convertView.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                BookCaseOnClick(position);
            }
        });

        viewHolder.image.setImageResource(R.drawable.book);//Create a drawable backup cover


        viewHolder.textView.setText( bi.getBook_Title());//Object refers to Book_information


viewHolder.textView.setBackgroundResource(R.drawable.alphabet_bar_bg_dark);
//          new LoadCover(image, bi).run();
            LoadCover(viewHolder.image, bi, position);
            return convertView;
        }

static class ViewHolder{
    ImageView image;
    TextView textView;
}

但是,在第一个循环加载后,我在这一行遇到错误。(第二个)

viewHolder = (ViewHolder) convertView.getTag();

这些是错误。需要注意的主要是在调试过程中发现异常并在gridview类上传递了“java.lang.integer”错误

08-16 20:13:02.073: E/AndroidRuntime(6520): FATAL EXCEPTION: main
08-16 20:13:02.073: E/AndroidRuntime(6520): java.lang.ClassCastException: java.lang.Integer
08-16 20:13:02.073: E/AndroidRuntime(6520):     at epub.activity.View_Book_Selection_Screen$BookCaseAdapter.getView(View_Book_Selection_Screen.java:835)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.AbsListView.obtainView(AbsListView.java:1409)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.GridView.makeAndAddView(GridView.java:1222)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.GridView.makeRow(GridView.java:268)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.GridView.fillDown(GridView.java:221)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.GridView.fillFromTop(GridView.java:340)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.GridView.layoutChildren(GridView.java:1136)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.AbsListView.onLayout(AbsListView.java:1260)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.View.layout(View.java:7175)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.View.layout(View.java:7175)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1243)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1049)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.View.layout(View.java:7175)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.View.layout(View.java:7175)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.View.layout(View.java:7175)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.View.layout(View.java:7175)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.View.layout(View.java:7175)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.os.Looper.loop(Looper.java:123)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at java.lang.reflect.Method.invokeNative(Native Method)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at java.lang.reflect.Method.invoke(Method.java:507)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-16 20:13:02.073: E/AndroidRuntime(6520):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

Akyl,类似的东西可能会对你有所帮助。

ViewHolder holder = null;
if (convertView == null || !(convertView.getTag() instanceof ViewHolder)) {
    convertView = mInflater.inflate(R.layout.your_view, null);
    holder = new ViewHolder(your params);
    convertView.setTag(holder);
} else {
    holder = (ViewHolder) convertView.getTag();
}
于 2012-08-16T21:56:43.930 回答
1

您正在将整数设置为标签

convertView.setTag(position);

但是你正在做

viewHolder = (ViewHolder) convertView.getTag();

这就是您收到此异常的原因。将 viewHolder 设置为标签或将其转换为整数

于 2012-08-16T20:48:57.263 回答