1

我正在使用下一个自定义网格视图:https ://github.com/TonicArtos/StickyGridHeaders

我收到 ArrayIndexOutOfBound 的适配器有问题。列表大小为 3,标题数量为 2。getView() 方法适用于位置 3(第 4 个位置)。

The Exception is:
07-18 01:36:04.574: E/AndroidRuntime(27942): FATAL EXCEPTION: main
07-18 01:36:04.574: E/AndroidRuntime(27942): java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
07-18 01:36:04.574: E/AndroidRuntime(27942): at java.util.Vector.arrayIndexOutOfBoundsException(Vector.java:907)
07-18 01:36:04.574: E/AndroidRuntime(27942): at java.util.Vector.elementAt(Vector.java:328)
07-18 01:36:04.574: E/AndroidRuntime(27942): at java.util.Vector.get(Vector.java:442)
07-18 01:36:04.574: E/AndroidRuntime(27942): at fragments.SalesStickyGVAdapter.getItem(SalesStickyGVAdapter.java:39)
07-18 01:36:04.574: E/AndroidRuntime(27942): at fragments.SalesStickyGVAdapter.getView(SalesStickyGVAdapter.java:52)
07-18 01:36:04.574: E/AndroidRuntime(27942): at com.tonicartos.widget.stickygridheaders.StickyGridHeadersBaseAdapterWrapper.getView(StickyGridHeadersBaseAdapterWrapper.java:179)
07-18 01:36:04.574: E/AndroidRuntime(27942): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
07-18 01:36:04.574: E/AndroidRuntime(27942): at android.widget.GridView.makeAndAddView(GridView.java:1341)
07-18 01:36:04.574: E/AndroidRuntime(27942): at android.widget.GridView.makeRow(GridView.java:341)

我在适配器中的代码是:

public class SalesStickyGVAdapter extends BaseAdapter implements StickyGridHeadersBaseAdapter 
{
private Context mContext;
private Vector items;
private String [] headers;

public SalesStickyGVAdapter(Context mContext, String [] headers, Vector<SaleObj> sales)
{
    this.mContext = mContext;
    this.items = sales;

    this.headers = headers;
}

@Override
public int getCount()
{       
    return items.size();
}

@Override
public SaleObj getItem(int position) 
{   
    return items.get(position);
}

@Override
public long getItemId(int position) 
{       
    return items.get(position).hashCode();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{       
    ChildView holder;
    SaleObj currentSaleObj = getItem(position);

    if(convertView == null)
    {
        holder = new ChildView();
        convertView = LayoutInflater.from(mContext).inflate(R.layout.sale_row_child_layout, null);
        holder.tvPubName = (RobotoTextView)convertView.findViewById(R.id.tv_sale_pub_name);
        holder.tvSaleDescription = (RobotoTextView)convertView.findViewById(R.id.tv_sale_description);

        convertView.setTag(holder);
    }
    else
    {
        holder = (ChildView)convertView.getTag();
    }

    //get the pub name
    holder.tvPubName.setText(App.pubById.get(currentSaleObj.pubSaleRelation.toString()).pubName);
    holder.tvSaleDescription.setText(currentSaleObj.saleDescription);

    return convertView;
}

private class ChildView
{
    private RobotoTextView tvPubName;
    private RobotoTextView tvSaleDescription;
}

@Override
public int getCountForHeader(int position) 
{       
    int RetVal = 0;
    try 
    {
        boolean isSaleTemp = getItem(position).isTemporarySale;
        if(isSaleTemp)
        {
            RetVal = App.salesByType.get(headers[0]).size();
        }
        else
        {
            RetVal = App.salesByType.get(headers[1]).size();
        }
    }
    catch (Exception e) {}
    return RetVal;
}

@Override
public View getHeaderView(int position, View convertView, ViewGroup parent) 
{       
    HeaderView holder;
    String currentHeader = headers[position];
    if(convertView == null)
    {
        holder = new HeaderView();
        convertView = LayoutInflater.from(mContext).inflate(R.layout.sales_header_view, null);
        holder.tvHeader = (RobotoTextView)convertView.findViewById(R.id.tv_header_text);

        convertView.setTag(holder);
    }
    else
    {
        holder = (HeaderView)convertView.getTag();
    }

    holder.tvHeader.setText(currentHeader);     

    return convertView;
}

private class HeaderView
{
    private RobotoTextView tvHeader;
}

@Override
public int getNumHeaders()
{
    int RetVal = 0;
    try
    {
        RetVal = App.salesByType.keySet().size();
    }
    catch (Exception e) {}
    return RetVal;
}
}

我会很感激任何帮助。谢谢大家。

4

1 回答 1

0

问题出在这种方法中:

@Override
public int getCountForHeader(int position) 
{       
    int RetVal = 0;
    try 
    {
        boolean isSaleTemp = getItem(position).isTemporarySale;
        if(isSaleTemp)
        {
            RetVal = App.salesByType.get(headers[0]).size();
        }
        else
        {
            RetVal = App.salesByType.get(headers[1]).size();
        }
    }
    catch (Exception e) {}
    return RetVal;
}

我应该做的是:

@Override
public int getCountForHeader(int position) 
{       
    int RetVal = 0;
    try 
    {
       RetVal = App.salesByType.get(headers[position]).size()
    }
    catch (Exception e) {}
    return RetVal;
}
于 2013-07-20T12:01:40.530 回答