Question

I'm having some issues working with a CursorAdapter.

In bindView(), I retrieve data in this way:

final String id = c.getString(c.getColumnIndexOrThrow(MySQLiteHelper.PROF_CONTACTS_KEY_ID));        
final String name = c.getString(c.getColumnIndexOrThrow(MySQLiteHelper.PROF_CONTACTS_KEY_NAME));

Right after this code, I call

Log.e("Log",id+" <=> "+name);

But, because of some weird problem, I got as a result an ID moved forward by 1.

This is the situation in the DB (pulling it from the emulator, and opening it with SQLite Manager):

Database situation

And this is the output:

Logcat

With bigger numbers (>9), IDs start to mess even more up: number 10 becomes number 1, number 13 becomes number 5, etc. I wouldn't have a lot of problems, in fact the only thing not matching is the id, all other info correspond, but I have a details activity to which I pass the ID in order to show to the user the detailed info.

This is the piece of code where I apply the adapter:

    mCursor = mDb.rawGet("SELECT * FROM "+MySQLiteHelper.PROF_CONTACTS_TB_NAME+" LEFT JOIN "+
            MySQLiteHelper.EXAMS_TB_NAME+" ON "+
            MySQLiteHelper.PROF_CONTACTS_TB_NAME+"."+MySQLiteHelper.PROF_CONTACTS_KEY_COD_ESAME+"="+
                MySQLiteHelper.EXAMS_TB_NAME+"."+MySQLiteHelper.EXAMS_KEY_COD 
            + " ORDER BY " + MySQLiteHelper.PROF_CONTACTS_TB_NAME+"."+MySQLiteHelper.PROF_CONTACTS_KEY_ID);

    if (mCursor.getCount() == 0) {
        // error stuff.
    } else {

        String[] columns = new String[] {};
        int[] to = new int[] {};

        mDataAdapter = new CursorAdapterProfContacts(getSherlockActivity(), R.layout.item_prof_contact, mCursor, columns, to, 0);
        mLvContacts.setAdapter(mDataAdapter);

    }
Was it helpful?

Solution 3

It was due to a collision name: _id can be referred both to EXAMS and PROF. SQLlite chose EXAMS instead of PROF.

    mCursor = mDb.rawGet("SELECT *, "+
                    MySQLiteHelper.PROF_CONTACTS_TB_NAME+"."+MySQLiteHelper.PROF_CONTACTS_KEY_ID+" AS idProf "+
                    " FROM "+MySQLiteHelper.PROF_CONTACTS_TB_NAME+" LEFT JOIN "+
                    MySQLiteHelper.EXAMS_TB_NAME+" ON "+
                    MySQLiteHelper.PROF_CONTACTS_TB_NAME+"."+MySQLiteHelper.PROF_CONTACTS_KEY_COD_ESAME+"="+
                    MySQLiteHelper.EXAMS_TB_NAME+"."+MySQLiteHelper.EXAMS_KEY_COD +
                    " ORDER BY " + MySQLiteHelper.PROF_CONTACTS_TB_NAME+"."+MySQLiteHelper.PROF_CONTACTS_KEY_ID);

And finally

    final Long id = c.getLong(c.getColumnIndexOrThrow("idProf"));   

This made the trick. Collision name errors should be thrown, as it is in SQL and MySQL.

OTHER TIPS

Move the cursor to the first row,after initial cursor like, mCursor.moveToFirst()

Are you sure that you have _id correctly populated when you insert a value? You can extract the database if you use the emulator and open it with SQLiteManager plugin for Firefox. As well, instead of quering all with *, use the same projection column names as you use inside y our bindView(); something is not matching here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top