[英]Getting textView values from listView
我有一个listView
,它由sqlite
数据库中的simpleCursorAdapter
填充。 每个listView
条目都有3个textViews
:Value,hour和date。
我按日期值过滤listView cursor
,所以如果我在过滤器中输入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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.