我正在尝试从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;
}
基本上,用户可以通过firstname,lastname或last check up从微调器中选择来对列表进行排序。我试过上面的代码无济于事。我不知道代码有什么问题?还是我错过了什么?有什么想法吗?非常感谢帮助。感谢。
答案 0 :(得分:2)
试试这个
mCursor = myDataBase.query(DB_TABLE, new String[] {
KEY_ID, KEY_FNAME, KEY_LNAME, KEY_DIAGNOSIS, KEY_LASTFFUP },
null, null, null, null, KEY_LNAME + " ASC");
更多细节Here
答案 1 :(得分:0)
试试这个:
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);
希望它能帮助!!