无法投射持有人 - ListView

时间:2017-09-06 12:56:56

标签: android listview android-viewholder

我在Android中遇到了问题,我尝试了几种解决方案,但没有任何效果。

当我滚动列表时,我得到了例外:

java.lang.ClassCastException:com.example.restaurante.SmartMercadoriasAdapter $ FirstViewHolder无法强制转换为com.example.restaurante.SmartMercadoriasAdapter $ SecondViewHolder

这是我的代码:

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    if (tipo.equals("1"))
        return 0;
    else
        return 1;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    MyListSmartMercadoria mercadoria = null;
    if(mercadorias.size() > 1){
        mercadoria = mercadorias.get(position);
    }else{
        mercadoria = mercadorias.get(0);
    }
    tipo       = "";

    tipo = mercadoria.getTipo();

    int viewType = getItemViewType(position);

    switch (viewType) {

        case 0: {
            FirstViewHolder firstViewHolder = null;

            if(view == null){
                view = LayoutInflater.from(getContext()).inflate(R.layout.merc_lista_smart_adp, parent, false);
                firstViewHolder = new FirstViewHolder(view);
                view.setTag(firstViewHolder);
            }
            else {
                firstViewHolder = (FirstViewHolder)view.getTag();
            }

            firstViewHolder.mTipo.setText(tipo);

            String codigo = mercadoria.getCod();
            if(codigo.matches("[0-9]+")){
                int codI = Integer.parseInt(mercadoria.getCod());
                codigo = "[";
                codigo += String.format("%06d", codI);
                codigo += "]";
            }else{
                codigo = "[";
                codigo += codigo;
                int cont = 6 - codigo.length();
                for(int i = 0; i < cont; i++){
                    codigo = codigo + " ";
                }
                codigo += "]";
            }
            firstViewHolder.mCodigo.setText(codigo);

            firstViewHolder.mDescricao.setText(mercadoria.getNome());
            firstViewHolder.mPreco.setText(mercadoria.getPreco());

            break;
        }

        case 1: {
            SecondViewHolder holder = null;

            if(view == null){
                view = LayoutInflater.from(getContext()).inflate(R.layout.merc_sub_lista_smart_adp, parent, false);
                holder = new SecondViewHolder(view);
                view.setTag(holder);
            }
            else holder = (SecondViewHolder)view.getTag();

            holder.mTipo.setText(tipo);

            String codigo = mercadoria.getCod();
            if(codigo.matches("[0-9]+")){
                int codI = Integer.parseInt(mercadoria.getCod());
                codigo = "[";
                codigo += String.format("%06d", codI);
                codigo += "]";
            }else{
                codigo = "[";
                codigo += codigo;
                int cont = 6 - codigo.length();
                for(int i = 0; i < cont; i++){
                    codigo = codigo + " ";
                }
                codigo += "]";
            }
            holder.mCodigo.setText(codigo);

            holder.mDescricao.setText(mercadoria.getNome());
            holder.mQuant.setText(mercadoria.getPreco());

            break;
        }
    }

    return view;

}

protected class SecondViewHolder {
    TextView mTipo;
    TextView mCodigo;
    TextView mDescricao;
    EditText mQuant;

    public SecondViewHolder(View view) {
        mCodigo = (TextView) view.findViewById(R.id.text_view_cod_merc);
        mTipo = (TextView) view.findViewById(R.id.text_view_tipo_merc);
        mDescricao = (TextView) view.findViewById(R.id.text_view_nome_merc);
        mQuant = (EditText) view.findViewById(R.id.text_view_preco_merc);
    }
}

protected class FirstViewHolder {
    TextView mTipo;
    TextView mCodigo;
    TextView mDescricao;
    TextView mPreco;

    public FirstViewHolder(View view) {
        mCodigo = (TextView) view.findViewById(R.id.text_view_cod_merc);
        mTipo = (TextView) view.findViewById(R.id.text_view_tipo_merc);
        mDescricao = (TextView) view.findViewById(R.id.text_view_nome_merc);
        mPreco = (TextView) view.findViewById(R.id.text_view_preco_merc);
    }
}

EDIT1:此延伸发生异常:

if(view == null){
            view = LayoutInflater.from(getContext()).inflate(R.layout.merc_sub_lista_smart_adp, parent, false);
            holder = new SecondViewHolder(view);
            view.setTag(holder);
        }
        else holder = (SecondViewHolder)view.getTag();

特别是在 view.getTag()

1 个答案:

答案 0 :(得分:0)

在回收视图时不要存储字段

试试这个

@Override
public int getItemViewType(int position) {
    String tipo = mercadorias.get(position).getTipo();
    if (tipo.equals("1"))
        return 0;
    else
        return 1;
}

此外,这没有意义

if(mercadorias.size() > 1){
    mercadoria = mercadorias.get(position);
}else{  
    mercadoria = mercadorias.get(0);
}

如果Arraylist为空,则会抛出异常,但如果大小等于1,则该位置应该已经是第一个元素