2013-08-24 48 views
-1

我創建sectionviews GridView和有一些問題,它Android的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; 
} 

我可以解決我的問題嗎?

回答

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

我的textview還沒有ID!它創造了程序化。 –

+0

@ ozik.dev:如果你正在創建TextView programmicaly,那麼不需要用'(TextView)convertView'初始化TextView,你只需要在view = new TextView(mContext);'line –

+0

之前聲明TextView爲'TextView view'我創建的代碼就像你的提示,我有問題
我的GridView [EmptyView] [EmptyView]
[TextView] [EmptyView]當我滾動並再次來到這裏他改爲[TextView] [TextView] –

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