简体   繁体   English

从listView获取textView值

[英]Getting textView values from listView

I have a listView that gets filled by a simpleCursorAdapter from a sqlite database. 我有一个listView ,它由sqlite数据库中的simpleCursorAdapter填充。 Every listView entry has 3 textViews : Value,hour and date. 每个listView条目都有3个textViews :Value,hour和date。

I filter the listView cursor by the date value so if i enter 24-07-2013 in the filter it will only show the entries from the sqlite database that have the date set to that.It will sometimes show 1 or 3 or any number,depending on how many entries are in the database. 我按日期值过滤listView cursor ,所以如果我在过滤器中输入24-07-2013 ,它将只显示sqlite数据库中日期设置为该条目的条目。它有时会显示1或3或任何数字,取决于数据库中的条目数。

What i want to do is to add all the Values into a total and show it in a separate TextView or something. 我想要做的是将所有Values添加到总计中并在单独的TextView或其他内容中显示它。

So my question is : how do i get the Values from the listView and add them up ? 所以我的问题是:如何从listView获取Values并将其添加?

Here's my code,i'll try not to copy it all. 这是我的代码,我会尽量不复制它。

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());

And this is how i extract data from the database : 这就是我从数据库中提取数据的方式:

 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;
}

UPDATE : 更新:

I ended up doing this.It isn't the nicest solution i think but it does the job. 我最终做到了这一点。这不是我认为最好的解决方案,但它确实起到了作用。

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

So,when the input in the filter edittext gets changed,it runs another query in the database.If there's a better,more efficient way to do this please tell me...i really want to learn and of course,i don't want to make my app use too many resources. 因此,当过滤器edittext中的输入发生变化时,它会在数据库中运行另一个查询。如果有更好,更有效的方法,请告诉我......我真的想学习,当然,我不知道想让我的应用程序使用太多资源。

As my comment suggested, you could loop over the cursor after getting it from query, and this way your adapter doesn't have to do it. 正如我的评论建议的那样,你可以在从查询中获取光标后循环游标,这样你的适配器就不必这样做了。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM