在onBindViewHolder中为RecyclerView适配器实现不同的布局时获取类转换错误

时间:2016-10-06 11:21:47

标签: android android-layout android-studio android-fragments

以下是供您参考的代码..

public class RecyclerViewAdapterForHomeTab extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int TYPE_WEEKLY = 0;
private final int TYPE_EAT = 1;
private final int TYPE_EFORM = 2;
Context context;
private ArrayList<HomeTab> mData = new ArrayList();
private LayoutInflater mInflater;
private SharedPreferences sharedPref;

public RecyclerViewAdapterForHomeTab(Context context, ArrayList<HomeTab> briefDetailAgenda) {
    this.context = context;
    mInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mData = briefDetailAgenda;
    sharedPref = context.getSharedPreferences(context.getResources().getString(R.string.preference_file_key), Context.MODE_PRIVATE);
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    switch (viewType) {
        case TYPE_WEEKLY:  //Initializing type with image layout
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_tab_row_with_one_image_two_ll, parent, false);
            return new TypeWeekly(view);

        case TYPE_EAT: // Initializing type with out image layout
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_tab_row_with_image_one_ll, parent, false);
            return new TypeEat(view);

        case TYPE_EFORM: // Initializing type with out image layout
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_tab_row_wo_image, parent, false);
            return new TypeEForm(view);
    }
    return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    final HomeTab user_data = mData.get(position);
    if (user_data != null) {
        if (user_data.getType().matches("weektype")) {

            if (sharedPref.getInt("user_id", -1) == -1) {
                ((TypeWeekly) holder).tv_your_store.setText(context.getResources().getString(R.string.store_near_you));
                ((TypeWeekly) holder).tv_weekly_add_view_store.setText(context.getResources().getString(R.string.view_all_stores));
                ((TypeWeekly) holder).tv_store_name.setText(context.getResources().getString(R.string.login_select_store));
                ((TypeWeekly) holder).tv_strore_start_time.setText("");
            } else {
                if (sharedPref.getString("user_store_name", "").matches("")) {
                    ((TypeWeekly) holder).tv_your_store.setText(context.getResources().getString(R.string.store_near_you));
                    ((TypeWeekly) holder).tv_weekly_add_view_store.setText(context.getResources().getString(R.string.view_all_stores));
                    ((TypeWeekly) holder).tv_store_name.setText(context.getResources().getString(R.string.select_store));
                    ((TypeWeekly) holder).tv_strore_start_time.setText("");

                } else {
                    ((TypeWeekly) holder).tv_your_store.setText(context.getResources().getString(R.string.your_store));
                    ((TypeWeekly) holder).tv_weekly_add_view_store.setText(context.getResources().getString(R.string.view_store));
                    ((TypeWeekly) holder).tv_store_name.setText(sharedPref.getString("user_store_name", ""));
                    ((TypeWeekly) holder).tv_strore_start_time.setText(context.getResources().getString(R.string.store_time));
                }
            }

            ((TypeWeekly) holder).tv_weekly_ads.setText(context.getResources().getString(R.string.weekly_add));
            ((TypeWeekly) holder).tv_weekly_ad_msg.setText(context.getResources().getString(R.string.weekly_spl_for_you));
            ((TypeWeekly) holder).tv_weekly_add_view_all.setText(context.getResources().getString(R.string.view_all));
            ((TypeWeekly) holder).img_weeklyads.setImageResource(R.drawable.img_weekly);

        } else if (user_data.getType().matches("eattype")) {
            if (mData.get(position).getRowName().matches("eat")) {
                ((TypeEat) holder).type_eat_text1.setText(context.getResources().getString(R.string.eat_smart));
                ((TypeEat) holder).type_eat_text2.setText(context.getResources().getString(R.string.store_magazine_online));
                ((TypeEat) holder).tv_view_all.setText(context.getResources().getString(R.string.view_magazine));
                ((TypeEat) holder).img.setImageResource(R.drawable.img_eatsmart);
            } else if (mData.get(position).getRowName().matches("splocc")) {
                ((TypeEat) holder).type_eat_text1.setText(context.getResources().getString(R.string.spl_occasion));
                ((TypeEat) holder).type_eat_text2.setText(context.getResources().getString(R.string.order_online_spls));
                ((TypeEat) holder).tv_view_all.setText(context.getResources().getString(R.string.shop_now));
                ((TypeEat) holder).img.setImageResource(R.drawable.img_spl_occ);
            } else if (mData.get(position).getRowName().matches("fbevents")) {
                ((TypeEat) holder).type_eat_text1.setText(context.getResources().getString(R.string.fb_events));
                ((TypeEat) holder).type_eat_text2.setText(context.getResources().getString(R.string.follow_us_fb));
                ((TypeEat) holder).tv_view_all.setText(context.getResources().getString(R.string.view_all));
                ((TypeEat) holder).img.setImageResource(R.drawable.img_fb_events);
            } else if (mData.get(position).getRowName().matches("blog")) {
                ((TypeEat) holder).type_eat_text1.setText(context.getResources().getString(R.string.our_blog));
                ((TypeEat) holder).type_eat_text2.setText(context.getResources().getString(R.string.comment_share_connect));
                ((TypeEat) holder).tv_view_all.setText(context.getResources().getString(R.string.view_blog));
                ((TypeEat) holder).img.setImageResource(R.drawable.img_blog);
            }

        } else if (user_data.getType().matches("eformtype")) {
            if (mData.get(position).getRowName().matches("eform")) {
                ((TypeEForm) holder).tv_text1.setText(context.getResources().getString(R.string.employee_form));
                ((TypeEForm) holder).tv_text2.setText(context.getResources().getString(R.string.online_emploee_form));
                ((TypeEForm) holder).img_icon.setImageResource(R.drawable.ic_emp_form);

            } else if (mData.get(position).getRowName().matches("scrip")) {
                ((TypeEForm) holder).tv_text1.setText(context.getResources().getString(R.string.scrip_order_form));
                ((TypeEForm) holder).tv_text2.setText(context.getResources().getString(R.string.scrip_pro_order_form));
                ((TypeEForm) holder).img_icon.setImageResource(R.drawable.ic_scrip_order);
            } else if (mData.get(position).getRowName().matches("aboutmartins")) {
                ((TypeEForm) holder).tv_text1.setText(context.getResources().getString(R.string.about_martins));
                ((TypeEForm) holder).tv_text2.setText(context.getResources().getString(R.string.about_marins_msg));
                ((TypeEForm) holder).img_icon.setImageResource(R.drawable.ic_about_new);
            }
        }
    }
}

@Override
public int getItemCount() {
    if (mData == null)
        return 0;
    return mData.size();
}

//Differentiating item basd on getLarge method
@Override
public int getItemViewType(int position) {
    if (mData != null) {
        if (mData.get(position).getType().matches("weektype")) {
            return TYPE_WEEKLY;
        } else if (mData.get(position).getType().matches("eattype")) {
            return TYPE_EAT;
        } else if (mData.get(position).getType().matches("eformtype")) {
            return TYPE_EFORM;
        }
    }
    return 0;
}

//Defining item row with image
public static class TypeWeekly extends RecyclerView.ViewHolder {
    private ImageView img_weeklyads;
    private TextView tv_your_store, tv_weekly_add_view_store, tv_store_name, tv_strore_start_time, tv_weekly_ads, tv_weekly_add_view_all, tv_weekly_ad_msg;

    public TypeWeekly(View itemView) {
        super(itemView);
        tv_your_store = (TextView) itemView.findViewById(R.id.tv_your_store);
        tv_weekly_add_view_store = (TextView) itemView.findViewById(R.id.tv_weekly_add_view_store);
        tv_weekly_add_view_store = (TextView) itemView.findViewById(R.id.tv_weekly_add_view_store);
        tv_store_name = (TextView) itemView.findViewById(R.id.tv_store_name);
        tv_strore_start_time = (TextView) itemView.findViewById(R.id.tv_strore_start_time);
        tv_weekly_ads = (TextView) itemView.findViewById(R.id.tv_weekly_ads);
        tv_weekly_add_view_all = (TextView) itemView.findViewById(R.id.tv_weekly_add_view_all);
        tv_weekly_ad_msg = (TextView) itemView.findViewById(R.id.tv_weekly_ad_msg);
        img_weeklyads = (ImageView) itemView.findViewById(R.id.img_weeklyads);
    }
}

//Defining item row with out image
public static class TypeEat extends RecyclerView.ViewHolder {
    private TextView type_eat_text1, type_eat_text2, tv_view_all;
    private ImageView img;

    public TypeEat(View itemView) {
        super(itemView);
        img = (ImageView) itemView.findViewById(R.id.img_fb_events);
        type_eat_text1 = (TextView) itemView.findViewById(R.id.tv_text1);
        type_eat_text2 = (TextView) itemView.findViewById(R.id.tv_text2);
        tv_view_all = (TextView) itemView.findViewById(tv_fb_events_view_all);
    }
}

//Defining item row with out image
public static class TypeEForm extends RecyclerView.ViewHolder {
    private TextView tv_text1, tv_text2;
    private ImageView img_icon;

    public TypeEForm(View itemView) {
        super(itemView);
        img_icon = (ImageView) itemView.findViewById(R.id.img_icon);
        tv_text1 = (TextView) itemView.findViewById(R.id.tv_text1);
        tv_text2 = (TextView) itemView.findViewById(R.id.tv_text2);
    }
}

private void load_image(String url, int default_drawable, ImageView imageView) {
    Picasso.with(context)
            .load(url).fit()
            .placeholder(default_drawable)
            .error(default_drawable)
            .into(imageView);
}

}

我收到的错误是:: 无法将'com.martins.martinslvie.views.adapters.RecyclerViewAdapterForHomeTab $ TypeEat'投射到'com.martins.martinslvie.views.adapters.RecyclerViewAdapterForHomeTab.TypeWeekly'

请帮忙...

1 个答案:

答案 0 :(得分:0)

我认为你的代码是正确的但是在第一行你扩展了错误的类请更新此

 public class RecyclerViewAdapterForHomeTab extends RecyclerView.Adapter<RecyclerView.ViewHolder>

要,

   public class RecyclerViewAdapterForHomeTab extends RecyclerView.Adapter<RecyclerViewAdapterForHomeTab.ViewHolder>