RecyclerView多个布局会导致ClassCastException

时间:2016-01-26 17:43:33

标签: android android-recyclerview classcastexception

程序员!我尝试为itemType制作多个RecyclerView,我已经发现了类似的问题,但他们无法帮我解决问题。

代码:

public class RecyclerAdapter extends   RecyclerView.Adapter<RecyclerView.ViewHolder> implements AdListener {
protected List<FoodData> foodDataList;
protected Context context;
protected String image_url, food_title, id;
protected int ingredients_count;
protected Intent intent;
private static final String FROM = "c172x172";
private static final String TO = "c480x180";
private HashMap<Integer, String> titleMap = new HashMap<>();


public RecyclerAdapter(List<FoodData> food, Context context) {
    this.foodDataList = food;
    this.context = context;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType) {
        case 0:
            return new MainViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.test_add, parent, false));
        case 2:
            return new AdditionalHolderl(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false));

    }
    return null;
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    switch (holder.getItemViewType()) {

        case 0:
            MainViewHolder m_holder = (MainViewHolder) holder;
            image_url = foodDataList.get(position).getRecipe_image_url();
            image_url = image_url.replaceAll(FROM, TO);
            food_title = foodDataList.get(position).getRecipe_text();
            if (titleMap.containsKey(position)) {
                m_holder.title.setText(titleMap.get(position));
            } else {
                titleMap.put(position, food_title);
                updateTextViewUI(m_holder.title, food_title);
            }

            id = foodDataList.get(position).getRecipe_id();
            ingredients_count = foodDataList.get(position).getRecipe_ingr_count();

            Picasso.with(context)
                    .load(image_url)
                    .into(m_holder.food_picture);

            m_holder.title.setText(food_title);
            m_holder.ingr_count.setText("Количество ингридиентов: " + ingredients_count);
            m_holder.look.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    intent = new Intent(context, FoodAppearance.class);
                    intent.putExtra("id", foodDataList.get(position).getRecipe_id());
                    context.startActivity(intent);

                }
            });
            m_holder.food_picture.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    intent = new Intent(context, FoodAppearance.class);
                    intent.putExtra("id", foodDataList.get(position).getRecipe_id());
                    context.startActivity(intent);
                }
            });
            return;
    case 2:
            AdditionalHolderl new_holder = (AdditionalHolderl) holder;
            new_holder.lol.setText("Text");
            return;
        default:
            break;

    }


}


@Override
public int getItemCount() {
    return foodDataList.size();
}

private void updateTextViewUI(final TextView textView, final String title) {
    new Handler(context.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            textView.setText(title);

        }
    });
}


@Override
public int getItemViewType(int position) {
    return position % 2 * 2;
}


public class MainViewHolder extends RecyclerView.ViewHolder {

    protected ImageView food_picture;
    protected TextView title, ingr_count, look;
    protected LinearLayout linearLayout;

    public MainViewHolder(View view) {
        super(view);
        food_picture = (ImageView) view.findViewById(R.id.food_picture);
        title = (TextView) view.findViewById(R.id.title);
        ingr_count = (TextView) view.findViewById(R.id.ingr_count);
        look = (TextView) view.findViewById(R.id.look);
        linearLayout = (LinearLayout) view.findViewById(R.id.list_item);


    }

}

public class AdditionalHolderl extends RecyclerView.ViewHolder {
    protected TextView lol;

    public AdditionalHolderl(View view) {
        super(view);
        lol = (TextView) view.findViewById(R.id.lol);

    }
}
}

编辑

好的,新的LogCat:

  java.lang.IllegalArgumentException: Target must not be null.
                                                            at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)
                                                            at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
                                                            at be.motti.uzb.RecyclerAdapter.onBindViewHolder(RecyclerAdapter.java:80)

在这一行:

  Picasso.with(context)...

尝试删除/评论Picasso的实现,但接下来的元素为null并导致NPE

修改

我有debugged它,并且发现View中的所有MainViewHolder都是null?他们为什么?当我使用单ViewHolder时,它们不是 如果你帮助我,我会很高兴的。 提前谢谢!

3 个答案:

答案 0 :(得分:1)

对于视图类型,您的onBindViewHolder()似乎是错误的方式。在onCreateViewHolder()中,位置MainViewHolder会返回0,位置AdditionalViewHolder会返回2

0中交换2onBindViewHolder(),它应该有效。

答案 1 :(得分:1)

尝试

  

的instanceof

正确的

关键字!

在你的onBindViewHolder上做跟进(ViewHolder holder,int position)

    if(holder instanceof AdditionalViewHolder)
{
//Do your stuff for AdditionalViewHolder
}else{ //Do your stuff for MainViewHolder}

答案 2 :(得分:0)

将您的开关反转为onBindViewHolder。也就是说,在case 0上,处理MainViewHolder。这应该可以解决问题。