db.rawquery“排序依据”不起作用

db.rawquery“排序依据”不起作用,第1张

概述我想在数据库表中使用“ORDERBY”对所有数据库条目进行排序.我不知道我在做什么错,但这是行不通的.这是我的代码:classNoteHelperextendsSQLiteOpenHelper{privatestaticfinalStringDATABASE_NAME="note.db";privatestaticfinalintSCHEMA_VERSION=1;publ

我想在数据库表中使用“ 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);}@OverrIDepublic voID onCreate(sqliteDatabase db){    db.execsql("CREATE table Notes (_ID INTEGER PRIMARY KEY autoINCREMENT, note TEXT);");}@OverrIDepublic 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 onString noteID = null;@OverrIDeprotected 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; } };

解决方法:

尝试这种方式:

public boolean onoptionsItemSelected(MenuItem item) {    switch (item.getItemID()) {        case R.ID.Az:            dataset_cursor = helper.getAllSorted();            adapter.changeCursor(dataset_cursor);            return true;        case R.ID.Za:            // something to do            return true;        case R.ID.deleteall:            // something to do            helper.deleteall();            dataset_cursor.requery();            adapter.notifyDataSetChanged();            return true;        case R.ID.undo:            // something to do            return true;    }    return false;}

首先,您将新的排序结果分配给dataset_cursor,然后告诉适配器光标已更改. deleteall情况可能需要使用adapter.notifyDataSetChanged();.行添加到那里.

总结

以上是内存溢出为你收集整理的db.rawquery“排序依据”不起作用全部内容,希望文章能够帮你解决db.rawquery“排序依据”不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1085695.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存