开发者

Passing an Orderby

开发者 https://www.devze.com 2023-01-09 05:39 出处:网络
I have a list view with several columns; things such as name, date, etc. I want to be able to click on the header TextView and sort the list by that field. When the list loads the variable works, and

I have a list view with several columns; things such as name, date, etc. I want to be able to click on the header TextView and sort the list by that field. When the list loads the variable works, and a list is queried and sorted by the field _id (no surprise other than it works), but when i click on the header TextView i get a force close ::

Thread [<3> main] (Suspended (exception SQLiteException))

ViewRoot.handleMessage(Message) line: 1757

ViewRoot(Handler).dispatchMessage(Message) line: 99 Looper.loop() line: 123 ActivityThread.main(String[]) line: 4595

Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) l开发者_运维问答ine: not available [native method]

Method.invoke(Object, Object...) line: 521

ZygoteInit$MethodAndArgsCaller.run() line: 860

ZygoteInit.main(String[]) line: 618 NativeStart.main(String[]) line: not available [native method]

The TextView gives no errors when not changing my orderby variable.

SETTING VARIABLE:

private View.OnClickListener NameSortbtnListener = new View.OnClickListener(){

    public void onClick(View v){

            sort = " KEY_JOURNAL_TITLE ";
            fillData();
        }

    };

POPULATING LIST:

private void fillData() { Cursor notesCursor = mDbHelper.fetchAllJournals(sort); startManagingCursor(notesCursor);

        String[] from = new String[]{journalDbAdapter.KEY_JOURNAL_TITLE, 
                journalDbAdapter.KEY_LOCATION, journalDbAdapter.KEY_JDATE,

                journalDbAdapter.KEY_STEPS};

        int[] to = new int[]{R.id.text1, R.id.text2, R.id.text3, R.id.text4};


        SimpleCursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, 

from, to); setListAdapter(notes); }

QUERY (IN DB ADAPTER):

public Cursor fetchAllJournals(String sort) {

        return mDb.query(DATABASE_JOURNAL_TABLE, new String[] {KEY_JROWID, 
                KEY_JOURNAL_TITLE, KEY_JOURNAL_NOTES, KEY_JDATE, KEY_LOCATION,
                KEY_STEPS},null , null, null, null, sort ,null);
    }


Offhand, I don't know what your issue is. However, when you catch the exception you should be able to print out the message within the SQLiteException object which should help tracking down the problem.

Beyond, that I am assuming you have constants defined some where such as: String KEY_JROWID = "KEY_JROWID"; String KEY_JOURNAL_TITLE = "KEY_JOURNAL_TITLE";

In your onClick event you set your sort to " KEY_JOURNAL_TITLE " instead of KEY_JOURNAL_TITLE. If the column names don't match directly to the variable names SQLite may be throwing a column not found exception.

The only other possible issue I see is the random space before and after in the sort column. No idea, why you have it... so their could be a reason. And I really doubt it would cause a problem, but, just a shot in the dark.


i added an order string and combined them in the statement... the value of sort is "name" and the value of order is "ASC". both with out quotes stored in the string.

public Cursor fetchAllJournals(String sort, String order) {

        return mDb.query(DATABASE_JOURNAL_TABLE, new String[] {KEY_JROWID, 
                KEY_JOURNAL_TITLE, KEY_JOURNAL_NOTES, KEY_JDATE, KEY_LOCATION,
                KEY_STEPS},null , null, null, null, " " + sort + " " + order + " " ,null);
    }

0

精彩评论

暂无评论...
验证码 换一张
取 消