与SimpleCursorTreeAdapter绑定的ExpandableListView

时间:2014-03-08 13:37:49

标签: android expandablelistview simplecursortreeadapter

我尝试使用ExpandableListView绑定SimpleCursorTreeAdapter,但我正面临着正确绑定的问题。

我的Java代码:

void fillData() {
    orderDeliveryCursor = dbHelper.getOrderDelivery(order_id);

    Log.d(TAG, "Count "+orderDeliveryCursor.getCount());

    if(orderDeliveryCursor != null && orderDeliveryCursor.getCount() > 0) {
        // Cache the ID column index
        groupId = orderDeliveryCursor.getColumnIndexOrThrow(DatabaseHelper.ORDER_DELIVERY_SERVER_ID);

        // Set up our adapter
        adapter = new MyExpandableListAdapter(orderDeliveryCursor, context, 

            R.layout.frag_dispatch_header_layout,           // Header layout
            R.layout.frag_dispatch_order_product_child_layout,      // Dispatch Details Child Layout

            new String[] { DatabaseHelper.ORDER_DELIVERY_SERVER_ID, DatabaseHelper.ORDER_DELIVERY_INVOICE_ID, DatabaseHelper.ORDER_DELIVERY_TRANSPORT },    // group title for group layouts
            new int[] { R.id.dispatchHeaderDeliveryId, R.id.dispatchHeaderInvoice, R.id.dispatchHeaderTransport },

            new String[] { },   // exercise title for child layouts
            new int[] { });

        expandableListView.setAdapter(adapter);

        expandableListView.setVisibility(View.VISIBLE);
        errorTextView.setVisibility(View.GONE);
    } else {

        errorTextView.setVisibility(View.VISIBLE);
        expandableListView.setVisibility(View.GONE);    
    }
    }

    // extending SimpleCursorTreeAdapter
    public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {

    public MyExpandableListAdapter(Cursor cursor, Context context,
        int groupLayout, int childLayout, String[] groupFrom,
        int[] groupTo, String[] childrenFrom, int[] childrenTo) {
        super(context, cursor, groupLayout, groupFrom, groupTo,
            childLayout, childrenFrom, childrenTo);
    }

    // returns cursor with subitems for given group cursor
    @Override
    protected Cursor getChildrenCursor(Cursor groupCursor) {
        Cursor exercisesCursor = dbHelper.getOrderDeliveryProduct(groupCursor.getInt(groupId));
        Log.d(TAG, "child Count : "+exercisesCursor.getCount() + " : " +groupCursor.getInt(groupId)+ " : "+groupCursor.getInt(groupCursor.getColumnIndex(DatabaseHelper.ORDER_DELIVERY_SERVER_ID)));
        return exercisesCursor;
    }

    // I needed to process click on click of the button on child item
    @SuppressWarnings("deprecation")
    public View getChildView(final int groupPosition,
        final int childPosition, boolean isLastChild, View convertView,
        ViewGroup parent) {

        View rowView = super.getChildView(groupPosition, childPosition,
            isLastChild, convertView, parent);

        ((TextView) rowView.findViewById(R.id.dispatchInvoiceDateValue))
        .setText(orderDeliveryCursor.isNull(orderDeliveryCursor.getColumnIndex(DatabaseHelper.ORDER_DELIVERY_INVOICE_DATE)) 
            ? ""
                : orderDeliveryCursor.getString(orderDeliveryCursor.getColumnIndex(DatabaseHelper.ORDER_DELIVERY_INVOICE_DATE)));

        ((TextView) rowView.findViewById(R.id.dispatchInvoiceAmountValue))
        .setText(orderDeliveryCursor.isNull(orderDeliveryCursor.getColumnIndex(DatabaseHelper.ORDER_DELIVERY_INVOICE_AMOUNT)) 
            ? ""
                : orderDeliveryCursor.getString(orderDeliveryCursor.getColumnIndex(DatabaseHelper.ORDER_DELIVERY_INVOICE_AMOUNT)));

        ((TextView) rowView.findViewById(R.id.dispatchLRnoValue))
        .setText(orderDeliveryCursor.isNull(orderDeliveryCursor.getColumnIndex(DatabaseHelper.ORDER_DELIVERY_LR_NO)) 
            ? ""
                : orderDeliveryCursor.getString(orderDeliveryCursor.getColumnIndex(DatabaseHelper.ORDER_DELIVERY_LR_NO)));

        ((TextView) rowView.findViewById(R.id.dispatchLRDateValue))
        .setText(orderDeliveryCursor.isNull(orderDeliveryCursor.getColumnIndex(DatabaseHelper.ORDER_DELIVERY_LR_DATE)) 
            ? ""
                : orderDeliveryCursor.getString(orderDeliveryCursor.getColumnIndex(DatabaseHelper.ORDER_DELIVERY_LR_DATE)));

        /***
         * 
         * Process for INNER PRODUCTS
         * 
         */
        orderDeliveryProductCursor = dbHelper.getOrderDeliveryProduct(orderDeliveryCursor.getInt(orderDeliveryCursor.getColumnIndex(DatabaseHelper.ORDER_DELIVERY_SERVER_ID)));

        /**
         * Fill PRODUCT BY ORDERID
         */
        TableLayout productTableLayout = (TableLayout) rowView.findViewById(R.id.dispatchProductTableLayout);

        if(!dbHelper.db.isOpen())
        dbHelper.open();

        LinearLayout row;

        if(orderDeliveryProductCursor.getCount() > 0)
        { 
        int totalQty = 0;
        while (orderDeliveryProductCursor.moveToNext()) {
            String prodId = orderDeliveryProductCursor.getString(orderDeliveryProductCursor.getColumnIndex(DatabaseHelper.ORDER_PRODUCT_PRODUCT_ID));    

            /*** Creating Dynamic View for Product List ***/
            View childView = getActivity().getLayoutInflater().inflate(R.layout.frag_dispatch_product_list, null);

            TextView prodName;
            TextView prodQty;

            row = new LinearLayout(context);

            row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            prodName = (TextView) childView.findViewById(R.id.dispatchProductNameValue);
            prodName.setText(""+dbHelper.getProductName(prodId));

            int qty = orderDeliveryProductCursor.getInt(orderDeliveryProductCursor.getColumnIndex(DatabaseHelper.O_D_P_QUANTITY));

            totalQty += qty;

            prodQty = (TextView) childView.findViewById(R.id.dispatchProductQuantityValue);
            prodQty.setText(""+qty);

            row.addView(childView);
            productTableLayout.addView(row);

        }//while orderProductCursor.moveToNext()

        BigDecimal amt = new BigDecimal(totalQty).setScale(2, RoundingMode.DOWN);
        /*** for Total Amount ***/
        ((TextView) rowView.findViewById(R.id.dispatchQuantityTotal)).setText("Total : "+amt);
        }


        //if orderProductCursor.getCount() > 0
        return rowView;
    }

    }

输出:

我得到了这样的输出,我第一次正确

FirstTime:

enter image description here

第二次当我打开孩子时,我得到了:

enter image description here

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

两天后,我发现了我在getChildCursor();

中所犯的错误

我改变了:

// returns cursor with subitems for given group cursor
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
    Cursor exercisesCursor = dbHelper.getOrderDeliveryId(groupCursor.getInt(groupId));
    Log.d(TAG, "child Count : "+exercisesCursor.getCount() + " : " +groupCursor.getInt(groupId)+ " : "+groupCursor.getInt(groupCursor.getColumnIndex(DatabaseHelper.ORDER_DELIVERY_ORDER_ID)));
    return exercisesCursor;
}

每次我真正想要的时候返回1: