2012-08-14 70 views
3

我想在我的数据库表中使用“ORDER BY”命令我所有的数据库条目。 我不知道我在做什么错,但它不起作用。
这里我的代码:android-db.rawquery“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; 
} 



}; 
+0

你能解释一下“不起作用”的意思吗?如果没有崩溃阻止,'getAllSorted()'会给你一个排序结果。 – zapl 2012-08-14 16:51:36

+0

我的意思是没有崩溃,但没有发生。 – user1598550 2012-08-14 16:52:58

+0

您从该方法得到'null',或者您得到的游标为0行或者您的应用程序不显示任何内容?如果你不知道究竟发生了什么问题,那就无能为力了。 – zapl 2012-08-14 16:54:52

回答

1

尝试这种方式:

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();行。

+0

Fuaa像一个BOSS!这工作!非常感谢你! – user1598550 2012-08-14 18:24:47

+0

那么,请记住,cursor.requery()现在已被弃用) – UnknownJoe 2013-12-22 08:34:32

1

我不知道,但这里有一些猜测/东西去尝试。

1)注意和COLLATE之前似乎有两个空格。不知道这是否有所作为。 (事实上​​,在该SQL字符串的几个不同位置中似乎有额外空间)

2)当您删除COLLATE NOCASE子句时会发生什么?只是想知道这是否是问题,或者是否是实际的ORDER BY子句失败。

3)COLLATE NOCASE后需要指定ASC/DESC吗?

+0

我做了所有,但是=没有任何反应,也没有崩溃:S – user1598550 2012-08-14 17:02:02

+1

向我们显示调用此getAllSorted()的代码。你怎么知道它不工作?你把它映射到一个用户界面?在日志中打印出来? – Gophermofur 2012-08-14 17:27:38

2
dataset_cursor = helper.getAll(); 

您没有使用getAllSorted() ...

此外,onOptionsItemSelected()不会被调用的任何地方,即使是,没有被使用的返回值来调用getAllSorted(),无论是。

+0

对不起,我dint复制,但我有onOptionItemSelected抱歉,但= =不工作:S – user1598550 2012-08-14 18:02:40