java - 从 listView 获取 textView 值

标签 java android sqlite listview

我有一个 listView,它由 sqlite 数据库中的 simpleCursorAdapter 填充。 每个 listView 条目都有 3 个 textViews :值、小时和日期。

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

我想要做的是将所有Values添加到总数中,并在单独的TextView或其他东西中显示它。

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

这是我的代码,我会尽量不复制它。

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[] {};
    }
}

因此,当过滤器 edittext 中的输入发生更改时,它会在数据库中运行另一个查询。如果有更好、更有效的方法来执行此操作,请告诉我...我真的很想学习,当然,我不想让我的应用使用太多资源。

最佳答案

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

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
}

关于java - 从 listView 获取 textView 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17845246/

相关文章:

java - 如果数字在数字列表中,如何形成 sqlite 查询来选择一行

java - GUI 在 EDT 上显示

android - 有没有一种方法可以捕获所有错误而又不会在try语句中乱扔我的代码?

java - 业务代表与服务定位器

android - 如何使用requestSync延迟syncAdapter的执行

android - 在应用设置中保存用户信息

android - 游标索引越界异常(android SQLite)

java - sqlite 返回表中的随机记录

java - 我的 ListView 不断使用 LogoLoader Asynctask 刷新图像

java - 如何避免在运行 "test"时再次运行 "install"