Android:ListView适配器有问题吗?

时间:2017-06-20 09:32:11

标签: android android-viewholder

public class SwipeListAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<Sell> sellList;
    private String[] bgColors;

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;

    public SwipeListAdapter(Activity activity, List<Sell> sellList) {
        this.activity = activity;
        this.sellList = sellList;
    }

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

    @Override
    public Object getItem(int location) {
        return sellList.get(location);
    }

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

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

        int rowType = sellList.get(position).isHeaderSection;
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            switch (rowType) {
                case TYPE_SEPARATOR: {
                    convertView = LayoutInflater.from(activity.getApplicationContext())
                            .inflate(R.layout.section_header,parent, false);
                    holder.SectionHeaderTitle = (TextView) convertView.findViewById(R.id.header_title);
                    holder.SectionHeaderPrice= (TextView) convertView.findViewById(R.id.header_price);
                }
                break;
                case TYPE_ITEM: {

                    convertView = LayoutInflater.from(activity.getApplicationContext())
                            .inflate(R.layout.list_row,parent, false);
                    holder.Count = (TextView) convertView.findViewById(R.id.Count);
                    holder.SellDataDate = (TextView) convertView.findViewById(R.id.SellDataDate);
                    holder.ProductName = (TextView) convertView.findViewById(R.id.ProductName);
                    holder.ProductTotalPrice = (TextView) convertView.findViewById(R.id.ProductTotalPrice);
                }
                break;
                default: {

                    convertView = LayoutInflater.from(activity.getApplicationContext())
                            .inflate(R.layout.section_header,parent, false);
                    holder.SectionHeaderTitle = (TextView) convertView.findViewById(R.id.header_title);
                    holder.SectionHeaderPrice= (TextView) convertView.findViewById(R.id.header_price);
                }

            }
            convertView.setTag(holder);
        } else holder = (ViewHolder)convertView.getTag();
        /*String color = bgColors[position % bgColors.length];
        Count.setBackgroundColor(Color.parseColor(color));*/
        switch (rowType){
            case TYPE_SEPARATOR: {
                holder.SectionHeaderTitle.setText(sellList.get(position).SectionHeaderTitle);
                holder.SectionHeaderPrice.setText("Нийт: " + sellList.get(position).SectionHeaderAmount);
            } break;
            case TYPE_ITEM:{

以下行总是给出Null Pointer Exception。我应该更改自定义适配器还是其他部分?

holder.Count.setText(String.valueOf(sellList.get(position).Count));
                holder.SellDataDate.setText(String.valueOf(sellList.get(position).SellDataDate));
                holder.ProductName.setText(String.valueOf(sellList.get(position).ProductName));
                holder.ProductName.setHint(String.valueOf(sellList.get(position).LotteryNo));
                holder.ProductTotalPrice.setText(sellList.get(position).ProductTotalPrice + "₮");
                holder.ProductTotalPrice.setHint(sellList.get(position).BillNo);
            } break;
            default:{
                holder.SectionHeaderTitle.setText(sellList.get(position).SectionHeaderTitle);
                holder.SectionHeaderPrice.setText("Нийт: " + sellList.get(position).SectionHeaderAmount);
            }
        }

        return convertView;
    }

    public static class ViewHolder {
        public TextView Count;
        public TextView SellDataDate;
        public TextView ProductName;
        public TextView ProductTotalPrice;
        public TextView SectionHeaderTitle;
        public TextView SectionHeaderPrice;
    }

}

我使用视图持有者错误的方式吗?持有人给我空指针异常。或者是我的其他代码错误? ... 如果我这样写代码工作但很慢。

convertView = inflater.inflate(R.layout.list_row,null);
                holder.Count = (TextView) convertView.findViewById(R.id.Count);
                holder.SellDataDate = (TextView) convertView.findViewById(R.id.SellDataDate);
                holder.ProductName = (TextView) convertView.findViewById(R.id.ProductName);
                holder.ProductTotalPrice = (TextView) convertView.findViewById(R.id.ProductTotalPrice);

            holder.Count.setText(String.valueOf(sellList.get(position).Count));
            holder.SellDataDate.setText(String.valueOf(sellList.get(position).SellDataDate));
            holder.ProductName.setText(String.valueOf(sellList.get(position).ProductName));
            holder.ProductName.setHint(String.valueOf(sellList.get(position).LotteryNo));
            holder.ProductTotalPrice.setText(sellList.get(position).ProductTotalPrice + "₮");
            holder.ProductTotalPrice.setHint(sellList.get(position).BillNo);

'''

0 个答案:

没有答案