在CustomAdapter中使用单例设计模式时抛出类异常抛出异常

时间:2016-03-15 13:49:50

标签: android listview singleton classcastexception

我有自定义适配器的列表视图。我的自定义适配器使用两个不同的模型类,即ShipmentMasterDaoShipmentStatusDao

所以我使用了名为ShipmentStatus Singleton的Singleton类来返回ShipmentMasterDao objectShipmentMasterDao object

在我的自定义适配器中,我使用我之前创建的单例类访问ShipmentMasterDaoShipmentStatusDao的方法,但它在类强制转换异常错误中返回。

以下是我的单身人士课程,

public class ShipmentStatusSingleton  {

    private ShipmentMasterDao shipmentMasterDao;
    private ShipmentStatusDao shipmentStatusDao;

    private static ShipmentStatusSingleton ourInstance = new ShipmentStatusSingleton();

    public static ShipmentStatusSingleton getInstance() {
        return ourInstance;
    }

    private ShipmentStatusSingleton() {
    }

    public ShipmentStatusSingleton(ShipmentMasterDao shipmentMasterDao) {
        this.shipmentMasterDao = shipmentMasterDao;
    }

    public ShipmentMasterDao getShipmentMasterDaoInstance() {
        ShipmentMasterDao shipmentMasterDao = new ShipmentMasterDao();
        return shipmentMasterDao;
    }

    public ShipmentStatusDao getShipmentStatusDaoInstance() {
        return new ShipmentStatusDao();
    }
}

在我的自定义适配器和getView方法中,我按如下方式调用方法,

ShipmentStatusSingleton item = listForView.get(position);

        mViewHolder.mstatus.setText(ShipmentStatusSingleton.getInstance().getShipmentMasterDaoInstance().getStatus());
        mViewHolder.mDate.setText(ShipmentStatusSingleton.getInstance().getShipmentStatusDaoInstance().getDate())

问题是,它会在ShipmentStatusSingleton item = listForView.get(position);

上触发类强制转换异常

以下是错误日志

  

java.lang.ClassCastException:com.vaighai.model.ShipmentMasterDao   无法投射到com.vaighai.singleton.ShipmentStatusSingleton                                                          在com.vaighai.adapters.StatusAdapter.getView(StatusAdapter.java:84)                                                          在android.widget.AbsListView.obtainView(AbsListView.java:2627)

我的适配器代码如下,

public class StatusAdapter extends BaseAdapter {

    private Context mContext;
    private View view;
    private ViewHolder mViewHolder;
    private LayoutInflater layoutInflater;
    private ArrayList<ShipmentStatusSingleton> listForView;

    public StatusAdapter(Context mContext, ArrayList list) {
        super();
        mContext = mContext;
        listForView = list;
        layoutInflater = LayoutInflater.from(mContext);
    }


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

    @Override
    public Object getItem(int position) {
        return listForView.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

    //Viewholder class to contain inflated xml views
    private class ViewHolder {
        private TextView mstatus, mDate;
        private ImageView image;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        view = convertView;

        if (convertView == null) {
            //inflate the view for each row of listview
            view = layoutInflater.inflate(R.layout.status_progress, null);
            //ViewHolder object to contain myadapter.xml elements
            mViewHolder = new ViewHolder();
            mViewHolder.image = (ImageView) view.findViewById(R.id.progressIcon);
            mViewHolder.mDate = (TextView) view.findViewById(R.id.date);
            mViewHolder.mstatus = (TextView) view.findViewById(R.id.status);

            view.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) view.getTag();
        }
        ShipmentMasterDao shipmentMasterDaoInstance = ShipmentStatusSingleton.getInstance().getShipmentMasterDaoInstance();
        ShipmentStatusDao shipmentStatusDaoInstance = ShipmentStatusSingleton.getInstance().getShipmentStatusDaoInstance();

        ShipmentMasterDao item = listForView.get(position);

        mViewHolder.mstatus.setText(ShipmentStatusSingleton.getInstance().getShipmentMasterDaoInstance().getStatus());
        mViewHolder.mDate.setText(ShipmentStatusSingleton.getInstance().getShipmentStatusDaoInstance().getDate());

        return view;
    }
}

0 个答案:

没有答案