我有一个listview,其中adapter在Fragment中扩展了BaseAdapter。在该listview上,我实现了scrollview侦听器,该侦听器检测用户何时到达listview的末尾。在这种情况下,将加载更多数据。另外,我在该片段上有一个按钮,单击该按钮将重置所有过滤器并重新加载包含开始项的列表。问题是当用户在底部滚动并且同时(在加载新元素之前)按下按钮应用程序时将崩溃,并显示以下错误:

java.lang.Boolean cannot be cast to ba.store.models.Merchants


这是分解的代码:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    Integer identity;
    ViewHolder viewHolder = null;
    notifyDataSetChanged();
    if (convertView == null) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sales_place_item, parent, false);
        viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);

    } else {
        view = convertView;
        viewHolder = (ViewHolder) view.getTag();

    }
    Merchants merchants = (Merchants) getItem(position);
    if (merchants != null) {
        identity = merchants.getMerchantId();
        viewHolder.merchant_name.setText(merchants.getMerchantName());
        viewHolder.merchant_category.setText(merchants.getCategoryName().toUpperCase());
        viewHolder.border_limit.setText(merchants.getBorderLimitAmount().toString() + " KM");
        viewHolder.end_loyalty.setText(merchants.getEndLoyaltyPoints().toString());
        viewHolder.begin_loyalty.setText(merchants.getStartLoyaltyPoints().toString());

        if (merchants.getImagePath().equals(""))
            Picasso.with(view.getContext()).load(R.drawable.placeholder_sales).fit().into(viewHolder.merchant_image);
        else
            Picasso.with(view.getContext()).load(merchants.getImagePath()).fit().into(viewHolder.merchant_image);

    }


    return view;

}


getItem方法:

@Override
public Object getItem(int position) {
    if (merchants.size() > 0) {
        return merchants.get(position);
    } else
        return false;
}

最佳答案

问题是您的getItem()方法。

如果没有要显示的数据,则该方法将返回false(布尔值)。这会中断getview()中的转换。

getItem()方法更改为此:

@Override
public Object getItem(int position) {
    if (merchants.size() > 0)
        return merchants.get(position);
    else
        return null;
}


getView()中,替换为:

 Merchants merchants = (Merchants) getItem(position);
    if (merchants != null) {
        identity = merchants.getMerchantId();
        ...


与:

if (getItem(position) != null) {
    Merchants merchants = (Merchants) getItem(position);
    identity = merchants.getMerchantId();
    ...


这将解决崩溃问题,但您还必须检查merchants.getSize()为何为0。

关于java - java.lang.Boolean无法转换为ba.store.models.Merchants,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39765772/

10-10 09:57