我正在尝试从 sqlite db 对我的 listview 项目进行排序。
这是我迄今为止尝试过的:
final String[] categories = {
"Firstname", "Lastname", "Last check up"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewallpatients);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_spinner_item, categories);
spinner = (Spinner) findViewById (R.id.spCategory);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
final String f = parent.getItemAtPosition(position).toString();
switch(position){
case 0:
ca.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return dbHelper.sortByFirstname(f);
}
});
break;
case 1:
ca.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return dbHelper.sortByLastname(f);
}
});
break;
case 2:
ca.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return dbHelper.fetchPatientsByName(constraint.toString());
}
});
break;
}
}
DBHelper.class
public Cursor sortByFirstname(CharSequence inputText) throws SQLException {
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = myDataBase.query(DB_TABLE, new String[] {
KEY_ID, KEY_FNAME, KEY_LNAME, KEY_DIAGNOSIS, KEY_LASTFFUP },
null, null, null, null, null);
}
else {
mCursor = myDataBase.query(true, DB_TABLE, new String[] {
KEY_ID, KEY_FNAME, KEY_LNAME, KEY_DIAGNOSIS, KEY_LASTFFUP },
KEY_FNAME + " like '%" + inputText + "%'", null,
null, null, "FirstName asc", null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor sortByLastname(CharSequence inputText) throws SQLException {
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = myDataBase.query(DB_TABLE, new String[] {
KEY_ID, KEY_FNAME, KEY_LNAME, KEY_DIAGNOSIS, KEY_LASTFFUP },
null, null, null, null, null);
}
else {
mCursor = myDataBase.query(true, DB_TABLE, new String[] {
KEY_ID, KEY_FNAME, KEY_LNAME, KEY_DIAGNOSIS, KEY_LASTFFUP },
KEY_FNAME + " like '%" + inputText + "%'", null,
null, null, "LastName asc", null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
基本上,用户可以通过从微调器中按名字、姓氏或上次检查来对列表进行排序。我试过做上面的代码无济于事。不知道代码有什么问题?还是我错过了什么?有什么想法吗?非常感谢您的帮助。谢谢。