【发布时间】:2015-12-19 13:55:49
【问题描述】:
在 MainActivity 中,我希望 listView 显示特定月份的数据。
public void BuildList()
{
sqlcon.open(); // object of InfoAPI
Calendar cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH)+1;
Toast.makeText(getApplicationContext(),month+"",Toast.LENGTH_LONG).show();
Cursor cursor1=sqlcon.readData(month);
String[] columns=new String[]{
MyDatabaseHelper.Date,MyDatabaseHelper.TimeIn_Info,MyDatabaseHelper.TimeOut_Info
};
int[] to=new int[]
{
R.id.date,R.id.timeIn,R.id.timeOut
};
dataAdapter = new SimpleCursorAdapter(this, R.layout.retrieve_data,
cursor1,
columns,
to,
0);
dataAdapter.setViewBinder(new ViewBinder());
listView.setAdapter(dataAdapter);
}
}
InfoAPI
public Cursor readData(int m)
{
Cursor c=database.rawQuery(" SELECT _id, Date,Weather,Status, TimeIn_Info, TimeOut_Info FROM " + MyDatabaseHelper.TABLE_INFO +
" WHERE Name = ? and strftime('%m',Date)= ?",
new String[]{"LCV"}, new String[]{String.valueOf(m)},null);
if (c != null) {
c.moveToFirst();
}
return c;
}
错误
Error:(54, 26) error: no suitable method found for rawQuery(String,String[],String[],<null>)
method SQLiteDatabase.rawQuery(String,String[]) is not applicable
(actual and formal argument lists differ in length)
method SQLiteDatabase.rawQuery(String,String[],CancellationSignal) is not applicable
(actual and formal argument lists differ in length)
Error:(106, 30) error: method readData in class InfoAPI cannot be applied to given types;
required: int
found: no arguments
reason: actual and formal argument lists differ in length
我对 rawQuery 有点困惑。为了根据两个条件检索数据,正确的编写方法是什么?
已编辑
public Cursor readData(int m)
{
Cursor c=database.rawQuery(" SELECT _id, Date,Weather,Status, TimeIn_Info, TimeOut_Info FROM " + MyDatabaseHelper.TABLE_INFO +
" WHERE Name = ? and strftime('%m',Date)= ?",
new String[]{ "LCV", String.valueOf(m) }, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
对于日期列,它保存日期格式19-12-2015。现在没有错误,但 ListView 上没有数据显示。
【问题讨论】:
-
有错误吗?我认为您需要将参数放在单个 String[] 中的查询中
-
是的,请参阅,“找不到合适的方法”。这对你意味着什么?
-
@cricket_007 是因为
strftime吗? -
不,看documentation for rawQuery。有多少个参数?它们是什么类型的?这与您所拥有的有何不同?
-
我试图帮助您学习阅读文档。您的错误很明显-您没有正确调用该方法。因此,您应该首先查看 Android 文档,了解调用该方法的正确方法。
标签: android sqlite listview where-clause