Android gridview视图强制转换异常

时间:2013-08-24 06:15:39

标签: android gridview

我使用剖面图创建gridview并且遇到一些问题

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        int size = mArray.get(0).size();
        int z = size + (COLUMNS_NUM * 2);
//sectionView 
        if (position < (COLUMNS_NUM * 2) || (position > (z -1) && position < (z + COLUMNS_NUM))) {

            if(position == COLUMNS_NUM ){
                return header(R.string.grid_last_added, convertView);
            }

            if(position == z){
                return header(R.string.grid_last_added, convertView);
            }

            if (convertView == null) {
                convertView = new View(mContext);
            }
            // Set empty view with height of ActionBar
            convertView.setLayoutParams(new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight));

            if(position == COLUMNS_NUM || position == z){
                convertView.setLayoutParams(new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight - 15));
            }

            return convertView;
        }

        View v = convertView;
        if(v == null){
            v = LayoutInflater.from(mContext).inflate(R.layout.player_movie, null);
            v.setLayoutParams(mImageViewLayoutParams);
        }

        TextView title = (TextView) v.findViewById(R.id.serial_title);
        TextView seria = (TextView) v.findViewById(R.id.when);
        ImageView poster = (ImageView) v.findViewById(R.id.poster);

        //Offten.debug(getCount(), position);

        int x = (position - (COLUMNS_NUM * 2) )< mArray.get(0).size() ? position - (COLUMNS_NUM * 2) : position  - (z + COLUMNS_NUM);
        int y = (position - (COLUMNS_NUM * 2) )< mArray.get(0).size() ? 0 : 1;

//        Offten.debug(x + " " + (position < mArray.get(0).size() ? "true" : "false") + " " + mArray.get(0).size());



        //Offten.debug( Boolean.toString((position - (COLUMNS_NUM * 2) )< mArray.get(0).size()) + " " + x + " " + z);



        Serial serial = mArray.get(y).get(x);

        title.setText(serial.getTitle());
        seria.setText(serial.getSeria());
        mFetcher.loadImage( serial.getPoster(), poster);

        if(v.getLayoutParams().height != mItemHeight){
            v.setLayoutParams(mImageViewLayoutParams);
        }
        return v;
    }

    public TextView header(int message, View convertView){
        TextView view = (TextView) convertView; // issue with cast exception
        if(view == null){
            view = new TextView(mContext);
        }
        view.setText(underline(mContext.getResources().getString(message)));
        view.setTextSize(18);
        view.setTypeface(typeface, Typeface.BOLD_ITALIC);
        view.setTextColor(Color.WHITE);
        view.setGravity(Gravity.CENTER_VERTICAL);
        view.setLayoutParams(new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight - 15));
        return  view;
    }

使用此代码,我的标题TextView中始终存在强制转换异常的问题。 (视图无法转换为TextView)

@Override
public int getViewTypeCount() {
    // Two types of views, the normal ImageView and the top row of empty views
    return 2;
}

@Override
public int getCount() {
    int size = 0;
    for(List<Serial> s : mArray){
        size += s.size() + COLUMNS_NUM;
    }

    return size > 0 ? size + COLUMNS_NUM : size;
}

@Override
public int getItemViewType(int position) {

    int size = mArray.get(0).size();
    int z = size + (COLUMNS_NUM * 2);

    return position < (COLUMNS_NUM * 2) || (position > (z -1) && position < (z + COLUMNS_NUM)) ? 1 : 0;
}

我可以解决我的问题吗?

2 个答案:

答案 0 :(得分:0)

这样做

public TextView header(int message, View convertView){
        TextView view = (TextView) convertView.findViewById(R.id.<textviewId>); // issue with cast exception
        if(view == null){
            view = new TextView(mContext);
        }
        view.setText(underline(mContext.getResources().getString(message)));
        view.setTextSize(18);
        view.setTypeface(typeface, Typeface.BOLD_ITALIC);
        view.setTextColor(Color.WHITE);
        view.setGravity(Gravity.CENTER_VERTICAL);
        view.setLayoutParams(new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight - 15));
        return  view;
    }

答案 1 :(得分:-1)

所以我为我的问题答案!我有3个不同的视图,但我宣布只有2,我有问题!感谢所有人的帮助!

@Override
public int getItemViewType(int position) {

    int size = mArray.get(0).size();
    int z = size + (COLUMNS_NUM * 2);

    if (position < (COLUMNS_NUM * 2) || (position > (z -1) && position < (z + COLUMNS_NUM))) {

        if(position == COLUMNS_NUM || position == z){
            return 1;
        }

        return 2;
    }

    return 0;
}

    @Override
    public int getViewTypeCount() {
        // Two types of views, the normal ImageView and the top row of empty views
        return 3;
    }