如何添加"标题"由SimpleCursorAdapter

时间:2017-03-08 06:04:11

标签: java android expandablelistview simplecursoradapter

我在我的一个活动中使用ExpandableListView,并使用自定义SimpleCursorAdapter填充子视图和组视图(单独的.xml文件,每个文本视图有几个文本视图)。我希望具有以下功能:单击一个组时,子列表将填充,并且子列表的位置0中将填充另一个标题(两个文本视图),以充当每列数据的标题孩子显示。

这是我的SimpleCursorAdapter的代码以及我活动中相关代码的片段:

SimpleCursorAdapter:

public class PayeeCursorAdapter extends SimpleCursorTreeAdapter {

private final String LOG_TAG = getClass().getSimpleName();
private PayeeActivity mActivity;
protected final HashMap<Integer, Integer> mGroupMap;

// No cursor is added to the adapter so that it only runs when the CursorLoader runs, instead of every time the activity does
public PayeeCursorAdapter(
        Context context,        // The activity where the adapter will be running
        int groupLayout,        // The .xml layout file for the group layout
        int childLayout,        // The .xml layout file for the child layout
        String[] groupFrom,     // String of column names in the cursor that is the data for each group item
        int[] groupTo,          // The ID of the views in the group layout that display the column from the groupFrom String[]
        String[] childrenFrom,  // String of column names in the cursor that is the data for each child item
        int[] childrenTo) {     // The ID of the views in the child layout that display the column from the childFrom String[]

    super(context, null, groupLayout, groupFrom, groupTo, childLayout, childrenFrom, childrenTo);
    mActivity = (PayeeActivity) context;
    mGroupMap = new HashMap<Integer, Integer>();
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {

    int groupPos = groupCursor.getPosition();
    int groupId = groupCursor.getInt(groupCursor.getColumnIndex(BillMeContract.PayeeEntry._ID));

    Log.d(LOG_TAG, "getChildrenCursor() for groupPos " + groupPos);
    Log.d(LOG_TAG, "getChildrenCursor() for groupId " + groupId);

    mGroupMap.put(groupId, groupPos);
    Loader<Cursor> loader = mActivity.getSupportLoaderManager().getLoader(groupId);

    if(loader != null && !loader.isReset()) {
        mActivity.getSupportLoaderManager().restartLoader(groupId, null, mActivity);
    } else {
        mActivity.getSupportLoaderManager().initLoader(groupId, null, mActivity);
    }
    return null;
}

public HashMap<Integer, Integer> getGroupMap(){
    return mGroupMap;
}

}

的活动:

ExpandableListView expandablePayeeListView = (ExpandableListView) findViewById(R.id.payee_exp_list);

    mAdapter = new PayeeCursorAdapter(
            this,
            R.layout.list_group_payee,
            R.layout.list_item_payee_tx,
            new String[] {BillMeContract.PayeeEntry.COL_NAME},
            new int[] {R.id.payee_list_header},
            new String[] {BillMeContract.TransactionEntry.COL_DATE,
                          BillMeContract.TransactionEntry.COL_PAYMENT,
                          BillMeContract.TransactionEntry.COL_TYPE},
            new int[] {R.id.payee_list_item_date,
                       R.id.payee_list_item_amount,
                       R.id.payee_list_item_type});

    mAdapter.setViewBinder(new SimpleCursorTreeAdapter.ViewBinder() {

        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

            if(columnIndex == 4) {
                int type = cursor.getInt(columnIndex);
                TextView textView = (TextView) view;
                if(type == BillMeContract.TransactionEntry.TYPE_CASH) {
                    textView.setText(R.string.tx_spinner_type_cash);
                } else if (type == BillMeContract.TransactionEntry.TYPE_CHEQUE) {
                    textView.setText(R.string.tx_spinner_type_cheque);
                } else if (type == BillMeContract.TransactionEntry.TYPE_E_TRANSFER) {
                    textView.setText(R.string.tx_spinner_type_e_transfer);
                }
                return true;
            } else if(columnIndex == 3){
                String cost = String.format(Locale.CANADA, "%.2f", cursor.getDouble(columnIndex));
                cost = "$" + cost;
                TextView textView = (TextView) view;
                textView.setText(cost);
                return true;
            }
            return false;
        }
    });

    expandablePayeeListView.setAdapter(mAdapter);

0 个答案:

没有答案