滚动我的自定义gridView时应用程序崩溃

时间:2014-11-30 13:30:40

标签: android gridview scroll

enter image description here

我希望我的网格视图与图片中的网格视图类似,因此最右边列中的单元格只是按钮,其余单元格包含Imageview和textview

这是我的自定义适配器

public class AllCategoriesAdapter extends ArrayAdapter<Recipe>{

private ArrayList<Recipe> RecipesInfo;
private Context con;
private ImageLoader imageLoader;

public AllCategoriesAdapter(Context context, int resource, ArrayList<Recipe> objects) {
    super(context, resource, objects);
    // TODO Auto-generated constructor stub
    RecipesInfo         = objects;
    con                 = context;
    imageLoader         = new ImageLoader(context);
}

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    RecipeItemHolder recipeHolder;
    View view ;


    recipeHolder = new RecipeItemHolder(); 
    int type     = getItemViewType(position);
    if (convertView == null ) 
    {
        LayoutInflater inflater = ((Activity) con).getLayoutInflater();
        if ( type == 3 )
        {
            view                = inflater.inflate(R.layout.category_button_row, parent , false);

        }
        else
        {
            view                = inflater.inflate(R.layout.all_recipe_list_row, parent, false);

        }
        view.setTag(recipeHolder);
    }
    else
    {
        view = convertView;
        recipeHolder = (RecipeItemHolder) view.getTag();
    }

    Recipe rItem = (Recipe) this.RecipesInfo.get(position);

    if (  type == 3 )
    {
        recipeHolder.button = (Button) view.findViewById(R.id.btCategoryButton);
        recipeHolder.button.setText(rItem.getCategoryName());
    }
    else
    {
        recipeHolder.RecipeTxt1 = (TextView) view.findViewById(R.id.tvRecipeCell);
        recipeHolder.img        = (ImageView) view.findViewById(R.id.imgRecipeInCategory);
        recipeHolder.RecipeTxt1.setText(rItem.getName());
        /*Bitmap b = BitmapFactory.decodeResource(con.getResources(), R.drawable.ic_launcher);
        Drawable top = new BitmapDrawable(con.getResources(),b);
        recipeHolder.RecipeTxt1.setCompoundDrawablesWithIntrinsicBounds(null, top, null, null);*/
        imageLoader.DisplayImage(String.valueOf(rItem.getImageId()), recipeHolder.img);
    }


    return view;
}



public void notifyDataSetChanged() {
    // TODO Auto-generated method stub
    super.notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
    // TODO Auto-generated method stub
    return (position%4);
}
@Override
public int getViewTypeCount() {
    // TODO Auto-generated method stub
    return 2;
}
public void setArray (ArrayList<Recipe> obj)
{
    RecipesInfo = obj;
}

private static class RecipeItemHolder {
    TextView RecipeTxt1;
    Button   button;
    ImageView img;
}

}

但滚动时视图崩溃

11-30 13:19:07.030: E/AndroidRuntime(9593): FATAL EXCEPTION: main
11-30 13:19:07.030: E/AndroidRuntime(9593): java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
11-30 13:19:07.030: E/AndroidRuntime(9593):     at    android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6355)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4883)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3087)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3361)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.View.dispatchTouchEvent(View.java:7246)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.View.dispatchPointerEvent(View.java:7426)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4342)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4382)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.Choreographer.doFrame(Choreographer.java:530)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.os.Handler.handleCallback(Handler.java:725)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.os.Looper.loop(Looper.java:137)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at java.lang.reflect.Method.invokeNative(Native Method)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at java.lang.reflect.Method.invoke(Method.java:511)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-30 13:19:07.030: E/AndroidRuntime(9593):     at dalvik.system.NativeStart.main(Native Method)
11-30 13:19:11.040: E/Trace(9624): error opening trace file: No such file or directory (2)

1 个答案:

答案 0 :(得分:1)

您需要将getCount()添加到adapter

@Override
public int getCount() {
    return RecipesInfo.size();
}
enter code here

编辑:

您不需要使用getItemViewType et getViewTypeCount,因为您返回基于type = getItemViewType(position);

的视图