android - db.rawquery "order by"不工作

标签 android sqlite sql-order-by

我想在我的数据库表中使用“ORDER BY”来排序我所有的数据库条目。 我不知道我做错了什么,但它不起作用。
这是我的代码:

class NoteHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "note.db";
private static final int SCHEMA_VERSION = 1;


public NoteHelper(Context context){
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db){
    db.execSQL("CREATE TABLE Notes (_id INTEGER PRIMARY KEY AUTOINCREMENT, note TEXT);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    //no-op, since will not be called until 2nd schema
    //version exists
}

public void insert(String note) {

    ContentValues cv = new ContentValues();
    cv.put("note", note);
    //you must pass it at lease one name of a colum
    getWritableDatabase().insert("Notes", "note", cv);
}

public void update(String id, String note){
    ContentValues cv = new ContentValues();
    String[] args = {id};

    cv.put("note", note);
    getWritableDatabase().update("Notes", cv, "_id=?", args);
}

public void delete(String id){
    getWritableDatabase().delete("Notes", "_id=?", new String[] {id});
}

public Cursor getAll(){
    return (getReadableDatabase().rawQuery("SELECT _id, note FROM Notes",  null));
}


public Cursor getAllSorted(){
    return (getReadableDatabase().rawQuery("SELECT note  FROM Notes  ORDER BY note  COLLATE NOCASE", null));

}

public String getNote(Cursor c){
    return(c.getString(1));
}

public Cursor getById(String id) {
    String[] args = {id};

    return(getReadableDatabase().rawQuery("SELECT _id, note FROM Notes WHERE _id=?", args));
}

 }

如果有人知道问题出在哪里,请帮助我,谢谢。

这里是主要代码:

NoteAdapter adapter = null;
NoteHelper helper = null;
Cursor dataset_cursor = null;
EditText editNote = null;
//this is how track which note we are working on
String noteId = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    try{


    setContentView(R.layout.history);

    ListView list = (ListView)findViewById(R.id.list);
    editNote = (EditText)findViewById(R.id.myEditText);


    helper = new NoteHelper(this);
    dataset_cursor = helper.getAll();
    startManagingCursor(dataset_cursor);
    adapter = new NoteAdapter(dataset_cursor);
    list.setAdapter(adapter);
class NoteAdapter extends CursorAdapter {
    NoteAdapter(Cursor c){
        super(Ztutorial11.this, c);


    }




    public void bindView(View row, Context ctxt, Cursor c) {
        NoteHolder holder = (NoteHolder)row.getTag();
        holder.populateFrom(c, helper);
    }

    public View newView(Context ctxt, Cursor c, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.row, parent, false);
        NoteHolder holder = new NoteHolder(row);

        row.setTag(holder);
        return (row);
    }
}

static class NoteHolder {
    private TextView noteText = null;

    NoteHolder(View row){
        noteText = (TextView)row.findViewById(R.id.note);


    }

    void populateFrom(Cursor c, NoteHelper helper) {
        noteText.setText(helper.getNote(c));
    }
}



   public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater awesome = getMenuInflater();
awesome.inflate(R.menu.menu_expresions, menu);
return true;

   }
   public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.Az:

    //something to do
    helper.getAllSorted();
    //update the view
    dataset_cursor.requery();
    return true;
case R.id.Za:

    //something to do

    return true;
case R.id.deleteall:

    //something to do
    helper.deleteAll();
    dataset_cursor.requery();
    return true;
case R.id.undo:

    //something to do

    return true;

}

return false;
 }



 };

最佳答案

dataset_cursor = helper.getAll();

你没有使用 getAllSorted()...

此外,onOptionsItemSelected() 没有在任何地方被调用,即使被调用,调用 getAllSorted() 的返回值也没有被使用, 要么。

关于android - db.rawquery "order by"不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11956447/

相关文章:

python - SQLite 中字符串文字和标识符的区别

android.mk 复制一个bin到/system/xbin

android - Xamarin Android - 排序 iBeacon iCollection 导致异常

Android + 以编程方式通过蓝牙配对设备

android - Flutter 可以在任何 Android 平台版本上运行吗?

SQLiteDatabase 从 3 个表中选择

c# - 在Android设备上从Unity3D访问SQLite数据库时出现问题

sql-server - 在 SQL Server 中按多列排序

c# - 具有不同数量标准的“Then By”用法

mysql - 在 ORDER BY 中的字母后对数字进行排序