我想從sqlite數據庫中排序我的列表視圖項目。從sqlite中排序listview項目android
這是我到目前爲止已經試過:
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;
}
基本上,用戶可以通過名字,姓氏從旋轉選擇列表進行排序或最後檢查。我試着做上面的代碼無濟於事。我不知道代碼有什麼問題嗎?或者我錯過了什麼?任何想法傢伙?非常感謝幫助。謝謝。
如何直接執行查詢? 「SELECT」+ KEY_ID +「,」+ KEY_FNAME +「,」+ KEY_LNAME +「,」+ KEY_DIAGNOSIS +「,」+ KEY_LASTFFUP +「FROM」+ DB_TABLE +「WHERE」+ KEY_FNAME +「LIKE'%」+ inputText +「%'ORDER BY」+ KEY_FNAME「;」 – SKT