用DB游标查询自定义适配器

时间:2011-11-12 00:49:35

标签: android

我使用以下自定义适配器从数据库游标填充我的列表控件。 我无法理解为什么这个代码在构造函数中特别是在调用super时崩溃了。

public class ListAdaptor extends SimpleCursorAdapter {

    private Cursor dataCursor;
    private LayoutInflater mInflater;

    class ViewHolder {

        public TextView label   = null;
        public CheckBox chkBx   = null;
        public TextView price   = null;
        public TextView weight  = null;
    }


    //constructor
    public ListAdaptor(Context context, int layout, Cursor dataCursor, String[] from, int[] to) {

        super(context, layout, dataCursor, from, to);
        this.dataCursor = dataCursor;
        mInflater = LayoutInflater.from(context);
    }

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

        // A ViewHolder keeps references to children views to avoid unneccessary calls
        // to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is no need
        // to reinflate it. We only inflate a new View when the convertView supplied
        // by ListView is null.

        if (convertView == null) {

            // Inflate the view
            convertView = mInflater.inflate(R.layout.listviewlyt, null);

            // Get the ID's of the views
            TextView tmpLbl     = (TextView)convertView.findViewById(R.id.label);
            CheckBox tmpChkBx   = (CheckBox)convertView.findViewById(R.id.chkbox);
            TextView tmpPrc     = (TextView)convertView.findViewById(R.id.labelPrice);
            TextView tmpWt      = (TextView)convertView.findViewById(R.id.labelWt);


            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            holder = new ViewHolder();

            holder.label    = tmpLbl;
            holder.chkBx    = tmpChkBx;
            holder.price   = tmpPrc;
            holder.weight  = tmpWt;

            // Set the Tag
            convertView.setTag(holder);

        } 
        else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // Bind the data efficiently with the holder.
        // Cursor to current item

        dataCursor.moveToPosition(position);
        String keyWrd = dataCursor.getString(2);
        String price  = dataCursor.getString(3);

        TextView labelRef   = holder.label;
        CheckBox chbxRef    = holder.chkBx;
        TextView labelPrc   = holder.price;
        TextView labelWt    = holder.weight;

        labelRef.setText(keyWrd);
        labelPrc.setText(price);
        //chbxRef.setChecked(refObj.flag);
        //labelWt.setText(refObj.wt);

        return convertView;
    }
}

有人可以帮我找到原因吗?

1 个答案:

答案 0 :(得分:0)

很可能这一行可能会给出问题

mInflater = LayoutInflater.from(context);