0

我有一个由数据库中listView的 a 填充的。每个条目都有 3 个 :值、小时和日期。simpleCursorAdaptersqlitelistViewtextViews

listView cursor按日期值过滤,所以如果我24-07-2013在过滤器中输入,它只会显示 sqlite 数据库中日期设置为该日期的条目。有时会显示 1 或 3 或任何数字,具体取决于有多少条目数据库。

我想要做的是将所有的添加Values到一个总数中,并以单独的TextView或其他的方式显示它。

所以我的问题是:我如何从中获取Values并将listView它们相加?

这是我的代码,我尽量不要全部复制。

private void displayListView() {

    Cursor cursor = dbHelper.fetchAllCountries();

    // The desired columns to be bound
    String[] columns = new String[] {
            SQLiteT.DB_COLUMN_1_NAME3,
            SQLiteT.DB_COLUMN_2_NAME3,
            SQLiteT.DB_COLUMN_3_NAME3,

    };

    int[] to = new int[] {

    R.id.numeprod3,
    R.id.cantotala3, 
    R.id.ultimulpret3,

    };

    dataAdapter = new SimpleCursorAdapter(this, R.layout.country_info3,
            cursor, columns, to, 0);


    ListView listView = (ListView) findViewById(R.id.listView13);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);

myFilter = (EditText) findViewById(R.id.myFilter3);
    myFilter.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            dataAdapter.getFilter().filter(s.toString());
        }
    });

    dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            return dbHelper.fetchCountriesByName(constraint.toString());

这就是我从数据库中提取数据的方式:

 public Cursor fetchCountriesByName(String inputText) throws SQLException {
    Cursor mCursor = null;
    if (inputText == null || inputText.length() == 0) {
        mCursor = this.sqliteDBInstance3.query(DB_TABLE_NAME3, new String[] {KEY_ROWID,
                DB_COLUMN_1_NAME3, DB_COLUMN_2_NAME3, DB_COLUMN_3_NAME3
                }, null, null, null, null, null);

    } else {
        mCursor = this.sqliteDBInstance3.query(true, DB_TABLE_NAME3,
                new String[] {KEY_ROWID, DB_COLUMN_1_NAME3,
                        DB_COLUMN_2_NAME3, DB_COLUMN_3_NAME3 },
                DB_COLUMN_3_NAME3 + " like '%" + inputText + "%'", null,
                null, null, null, null);
    }
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;

}

public Cursor fetchAllCountries() {

    Cursor mCursor = this.sqliteDBInstance3.query(DB_TABLE_NAME3,
            new String[] {KEY_ROWID, DB_COLUMN_1_NAME3, DB_COLUMN_2_NAME3,
                    DB_COLUMN_3_NAME3 }, null, null, null, null,
            null);

    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

更新 :

我最终这样做了。这不是我认为最好的解决方案,但它确实有效。

//Added to this :
public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            dataAdapter.getFilter().filter(s.toString());
            totaluri= dbHelper.getAllCountries(s.toString());
            //ore=dbHelper.getAllCountries2(s.toString());
            int pff=totaluri.length;
            float totalulma = 0;
            for(int i=0;i<pff;i++){totalulma=totalulma+Float.parseFloat(totaluri[i]);}
            totaltotaluri.setText(String.format("Total: %.2f", totalulma));

//And created this method inside my database activity 
public String[] getAllCountries(String inputText) {
    Cursor cursor = this.sqliteDBInstance3.query(DB_TABLE_NAME3,
            new String[] { DB_COLUMN_1_NAME3 }, DB_COLUMN_3_NAME3 + " like '%" + inputText + "%'", null, null, null,
            null);

    if (cursor.getCount() > 0) {
        String[] str = new String[cursor.getCount()];
        int i = 0;

        while (cursor.moveToNext()) {
            str[i] = cursor.getString(cursor
                    .getColumnIndex(DB_COLUMN_1_NAME3));
            i++;
        }
        return str;
    } else {
        return new String[] {};
    }
}

所以,当过滤器编辑文本中的输入发生变化时,它会在数据库中运行另一个查询。如果有更好、更有效的方法来做到这一点,请告诉我......我真的很想学习,当然,我不想让我的应用使用太多资源。

4

1 回答 1

2

正如我的评论所建议的那样,您可以在从查询中获取光标后遍历光标,这样您的适配器就不必这样做。

public Cursor fetchAllCountries() {
    Cursor mCursor = this.sqliteDBInstance3.query(DB_TABLE_NAME3,
            new String[] {KEY_ROWID, DB_COLUMN_1_NAME3, DB_COLUMN_2_NAME3,
                DB_COLUMN_3_NAME3 }, null, null, null, null,
        null);

    if (mCursor != null) {
        updateSum(mCursor);
    }
    return mCursor;
}

private void updateSum(Cursor cursor) {
    int sum = 0;
    final int index = cursor.getColumnIndex(COLUMN_NAME);
    for (cursor.moveToFirst; !cursor.isAfterLast(); cursor.moveToNext()) {
        int value = cursor.getInt(index);
        sum += value;
    }
    // do something with sum
}
于 2013-07-24T21:52:34.343 回答