Android构造函数未定义

时间:2014-09-01 04:49:02

标签: java android sql constructor

我正在尝试为Android中的每个Swipe View执行不同的SQL语句。这是我的onCreateView的一部分:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    expensesCat = inflater.inflate(R.layout.dashboard_expensescat_frag,
            container, false);

    DatabaseAdapter mDbHelper = new DatabaseAdapter(this);
    mDbHelper.createDatabase();
    mDbHelper.open();
    TransactionRecController trc = new TransactionRecController(
            mDbHelper.open());

    trans_list = trc.getChartExpensesCat();
    openChart();
    mDbHelper.close();
    return expensesCat;
}

在我的控制器类中:

private SQLiteDatabase mDb;
public TransactionRecController(SQLiteDatabase mDb) {
    this.mDb = mDb;
}

public ArrayList<TransactionRecModel> getChartExpensesCat() {
    try {
        String sql = "SELECT SUM(tr.amount) AS total, c.categoryName FROM transactionRec tr " +
                "INNER JOIN category c ON tr.categoryID = c.categoryID " +
                "WHERE tr.type = 'W' GROUP BY c.categoryName";
        Cursor mCur = mDb.rawQuery(sql, null);
        Log.e(TAG, "Data Grab Success");
        if (mCur.getCount() != 0) {
            if (mCur.moveToFirst()) {
                do {
                    TransactionRecModel trm = new TransactionRecModel();

                    trm.setCategory(mCur.getString(mCur
                            .getColumnIndex("categoryName")));
                    trm.setAmount(mCur.getInt(mCur.getColumnIndex("total")));

                    transList.add(trm);
                } while (mCur.moveToNext());
            }
        }
        return transList;
    } catch (SQLException mSQLException) {
        throw mSQLException;
    }
}

但是,当我尝试初始化DatabaseAdapter时,收到此错误消息:

The constructor DatabaseAdapter(DashboardExpensesCat) is undefined

我想知道如何修复此错误,因为我无法更改控制器类中的构造函数。

提前致谢。

1 个答案:

答案 0 :(得分:2)

问题是你传递了片段的对象

DatabaseAdapter mDbHelper = new DatabaseAdapter(this);

此处this表示Fragment的实例或对象。在这里你必须传递一个活动的Context,当你在Fragment中初始化它然后你需要传递

DatabaseAdapter mDbHelper = new DatabaseAdapter(getActivity());