Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android-db.rawquery“;“订购”;不';行不通_Android_Sqlite_Sql Order By - Fatal编程技术网

android-db.rawquery“;“订购”;不';行不通

android-db.rawquery“;“订购”;不';行不通,android,sqlite,sql-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){

我想在我的数据库表中使用“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;
 }



 };

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

1) 在注释之后和校对之前似乎有两个空格。我不确定这是否有区别。(事实上,该SQL字符串的几个不同位置似乎都有额外的空格)

2) 删除COLLATE NOCASE子句时会发生什么情况?只是想知道这是问题所在,还是实际的ORDERBY条款失败了

3) 您是否需要在整理NOCASE后指定ASC/DESC

dataset_cursor = helper.getAll();
您没有使用
getAllSorted()

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

请尝试以下方法:

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\u cursor
,然后告诉适配器光标已更改。
deleteall
案例可能需要
适配器。notifyDataSetChanged()我在那里添加的行。

你能解释一下“它不工作”是什么意思吗
getAllSorted()
会给你一个排序结果,如果没有崩溃阻止它。我的意思是没有崩溃,但什么都没有发生。你从该方法得到的结果是
null
,或者你得到一个0行的光标,或者你的应用程序没有显示任何内容?如果您不知道到底出了什么问题,则无法帮助。当我单击我传递的按钮时,在主活动中很简单:helper.getAllSorted();我认为这是正确的,在prees之后没有任何事情发生,也没有崩溃。你不使用新数据更新用户界面吗?如果有一些
ListView
带有
CursorAdapter
的适配器,请将该适配器的光标更改为您刚获得的新光标,或者使用新光标将适配器替换为新的适配器。我做了所有的事,但is=没有发生任何事情,也没有崩溃:SShow us调用此getAllSorted()的代码。你怎么知道它不起作用?您是否将其映射到UI?在日志中打印出来?是的,很抱歉,我无法复制,但我已选择onOptionItemSelected抱歉,但=不起作用:请记住,游标.requery()现在已被弃用)