views:

49

answers:

2
+1  Q: 

Passing an Orderby

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) line: 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);
    }
A: 

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.

aepryus
in the database adapter yes there are constants for each field.I played around with the spacing still the same error. also: i may not have pointed out that I declared sort before calling the query and no error.private String sort = "null";
Brian
What about the message on SQLiteException? What if you replace sort = " KEY_JOURNAL_TITLE " with sort = KEY_JOURNAL_TITLE?
aepryus
I cant test it right now but i dont believe you can set a string without quotes
Brian
sort = "'KEY_JOURNAL_TITLE'";gets rid of the error but still does not sort.. added a single quote ', no action visually
Brian
A: 

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);
    }

Brian