本文介绍了ExpandableListView与SimpleCursorTreeAdapter结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图绑定 ExpandableListView
使用 SimpleCursorTreeAdapter
,但我现在面临的问题,以正确的绑定。
I have tried to bind ExpandableListView
using SimpleCursorTreeAdapter
but i'm facing problem to bind correctly.
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;
}
}
输出:
我得到了这样的输出,我第一次得到了正确
Output:
I got output like this, First time i got correctly
我怎样才能解决这个问题?
How can i solve this??
推荐答案
两天后,我发现我的错误,我在 getChildCursor()做;
After two days i found my mistake which i have done in 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,每次它其实我是想:
Which returns 1 everytime which i actually want:
这篇关于ExpandableListView与SimpleCursorTreeAdapter结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!