android - 在 textview 中显示结果 sqlite 查询
问题描述
我试图计算一个报告并在 texview "edt1" 中显示结果。但它不显示。
有 mydatabasehelper :
public void calculrapport(Argent a)
{
db = this.getWritableDatabase();
String query = "select sum(Entree) from Argent where date between \"datedebut\" and \"datefin\" ;";
Cursor cursor = db.rawQuery(query , null) ;
int count = cursor.getCount();
}
有我的类 Rapport.java :
public void onOKClick ( View v ) {
if (v.getId() == R.id.okrapport) {
EditText datedebut = (EditText) findViewById(R.id.datedebut);
EditText datefin = (EditText) findViewById(R.id.datefin);
String strdatedebut = datedebut.getText().toString();
String strdatefin = datefin.getText().toString();
Argent a = new Argent();
helper.calculrapport(a);
edt1.setText( );
}
}
提前致谢。
解决方案
假设查询按预期工作(特别是考虑到使用的变量具有适当的范围,这似乎不太可能尝试使用select sum(Entree) from Argent
进行测试而没有 datedebut 和 datefin 变量是否可以解析的复杂性,如果可以解析为可用值)然后你需要 :-
提取适当的值并从方法中返回值,然后使用返回的值设置 TextView 中的文本。
要返回值,该方法不应为 void,而应具有适当的返回类型(示例将使用字符串),
所以代替
public void calculrapport(Argent a)
, 使用public String calculrapport(Argent a)
(因此该方法需要返回一个字符串)要提取值,需要将光标移动到适当的行(应该只有一行,因为 sum 函数是聚合函数,并且只有一组(聚合函数在 gropus 上工作),即所有行)
因此,该方法可能是:-
public String calculrapport(Argent a)
{
String rv = "0"; //<<<< used to return 0 if no rows are selected by the query
db = this.getWritableDatabase();
String query = "select sum(Entree) from Argent where date between \"datedebut\" and \"datefin\" ;";
Cursor cursor = db.rawQuery(query , null) ;
if (cursor.moveToFirst()) {
rv = cursor.getString(0); //<<<< get the value from the 1st (only) column
}
cursor.close(); //<<<< Cursors should always be closed when done with
return rv;
}
使用返回值设置 TextView 而不是使用:-
helper.calculrapport(a);
edt1.setText( );
利用 :-
edt1.setText(helper.calculrapport(a));
或者 :-
String sum = helper.calculrapport(a);
edt1.setText(sum);
补充评论:-
问题出在 SQlite 查询中(从 Argent 中选择 sum(Entre) where date between \"datedebut\" and \"datefin\" ;) 正是当我们在类关系中调用“datedebut”和“datefin”时。爪哇
然后String query = "select sum(Entree) from Argent where date between \"datedebut\" and \"datefin\" ;";
解决:-
select sum(Entree) from Argent where date between "datedebut" and "datefin" ;
我相信,假设 datedebut 和 datefin 是字符串变量,并且它们采用有效的 SQLite 日期格式,例如它们可能是 2018-01-01 和 2018-02-01 (并且日期列中的值被格式化为有效SQLite 日期格式),您应该改为使用:-
String query = "select sum(Entree) from Argent where date between '" + datedebut + "' and '" + datefin +"' ;";
然后会解决类似的问题:-
SELECT sum(entree) FROM argent WHERE date BETWEEN '2018-01-01' AND '2018-02-01';
对于有效的 SQLite 日期格式;请参阅日期和时间函数
- 请注意,以上是原则代码,尚未经过测试,因此可能包含一些简单的输入错误。
推荐阅读
- asp.net-mvc - Internet Explorer 中的防伪令牌问题
- html - 在 html 中删除移动视图中的额外空白
- json - 如何使用 jq 更新 json 文档中的单个整数?
- reactjs - 当用户从反应 js 应用程序中的不同选项卡返回页面时调用函数
- ag-grid - 有人可以帮助我使用 ag-grid 的 tabToNextCell 方法的默认代码吗
- javascript - 动态表的可排序列(使用 DataTable 制作)
- laravel - 如何重置 Vue 组件?
- python - parts = func_name.split('.') TypeError: a bytes-like object is required, not 'str'
- c++ - CMake - 输出目录结构和 QML 模块
- java - Private Key is coming as Null from Keystore Object