• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Mapping subset of cursor columns to xml fields

ac4android

Well-Known Member
I am bringing back several columns from SQLite with a raw query, but I am only mapping a small subset to the corresponding xml-fields for display in my customized Listview. The others will be bundled so I can pass them onto the next activity depending on the row the users pick.

A/S is not liking it so far, mapping only a subset of columns from the cursor to the xml-fields.

Wonder what Android is complaining about, no where does it say I have to display all the fields I got back with my the cursor
 
My telepathic powers are weak today. ;)
(Need to see your code)
 
Sorry, multi-tasking is getting to me o_O
Here are screenshots of the debug sessions:
debugDepLatitude.JPG

and the output alongside the actual codes:
debugDepLatitude222.JPG

and finally the codes for this app, I have hi-lited the offending line in bold red. The values for the onListItemClick-parameters are correct, position=1 and id=1. I have tried other positions and ids in the SQLite table:

Code:
public class ListDeliveriesActivity extends ListActivity {

    private SQLiteDatabase db;
    private Cursor cursor;
    private View dlvheaderView;
    private String alicplate;
    private double asitelat;
    private double asitelng;
    private double adeplat;
    private double adeplng;
    private double acurrlat;
    private double acurrlng;
    private LatLng siteLatLng;
    public LatLng depotLatLng;
    public LatLng currentLatLng;
    private final String dlvQuery = "SELECT _id, Licenseplate, "+
            "DepLatitude, DepLongitude, " +
            "SiteLatitude, SiteLongitude, " +
            "Latitude, Longitude  FROM Drivers " +
            "WHERE Projectname = ? " +
            "ORDER BY Licenseplate;";
    [USER=1021285]@override[/USER]
    // We will be using a simple LISTVIEW here in this module
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // grab the projectname to identify all trucks working on this project
        Bundle b = new Bundle();
        b = getIntent().getExtras();
        String aprojectname  = b.getString("tagprjname");
        //
        String[] dbFields = new String[]{
                AssetLocationDBHelper.FLD_Licenseplate,
                AssetLocationDBHelper.FLD_Latitude,
                AssetLocationDBHelper.FLD_Longitude
        };
        int[] xmlFields = new int[] {
                R.id.licplate,
                R.id.currentlat,
                R.id.currentlng
        };
        ListView listDeliveries = getListView();
        LayoutInflater inflater = getLayoutInflater();
        dlvheaderView = inflater.inflate(R.layout.activity_list_deliveries, listDeliveries, false);
        // TODO
        // Cursor for this project's list of vehicle locations.
        try {
            SQLiteOpenHelper assetlocationDBhelper = new AssetLocationDBHelper(this);
            SQLiteDatabase db = assetlocationDBhelper.getReadableDatabase();
            cursor = db.rawQuery(dlvQuery, new String[]{aprojectname});
            CursorAdapter listAdapter = new SimpleCursorAdapter(this,
                    R.layout.deliveries_list,
                    cursor,
                    dbFields,
                    xmlFields,
                    0);
            // move the cursor to point to the first record
            listDeliveries.addHeaderView(dlvheaderView);
            listDeliveries.setAdapter(listAdapter);
        } catch (SQLiteException ex) {
            Toast toast = Toast.makeText(this, "ERROR(2) database unavailable", Toast.LENGTH_SHORT);
            toast.show();
        }
    }
    [USER=1021285]@override[/USER]
    protected void onResume(){
        super.onResume();
    }
    [USER=1021285]@override[/USER]
    public void onListItemClick(ListView listDeliveriesView,
                                View itemView,
                                int position,
                                long id) {
        cursor = (Cursor) listDeliveriesView.getItemAtPosition(position);
        alicplate = cursor.getString(cursor.getColumnIndexOrThrow("Licenseplate"));
        //alicplate = cursor.getString(0);
        // location of depot
        adeplat = cursor.getDouble(cursor.getColumnIndexOrThrow("DepLatitude"));
        //adeplat = cursor.getDouble(1);
        //adeplng = cursor.getDouble(cursor.getColumnIndexOrThrow("DepLongitude"));
        //adeplng = cursor.getDouble(3);
        //depotLatLng = new LatLng(adeplat,adeplng);
        // location of pour site
        /**
        asitelat = cursor.getDouble(cursor.getColumnIndexOrThrow("SiteLatitude"));
        asitelng = cursor.getDouble(cursor.getColumnIndexOrThrow("SiteLongitude"));
        siteLatLng = new LatLng(asitelat,asitelng);
        // current location of truck
        acurrlat = cursor.getDouble(cursor.getColumnIndexOrThrow("Latitude"));
        acurrlng = cursor.getDouble(cursor.getColumnIndexOrThrow("Longitude"));
        currentLatLng = new LatLng(acurrlat, acurrlng);
         ***/
        Toast toast = Toast.makeText(this, alicplate + String.valueOf(adeplat), Toast.LENGTH_LONG);
        toast.show();
        //makeBundle();
    }
    private void makeBundle(){
        Bundle args = new Bundle();
        args.putParcelable("depotLatLng", depotLatLng);
        args.putParcelable("siteLatLng", siteLatLng);
        args.putParcelable("currentLatLng", currentLatLng);
        //
        Intent intent = new Intent(ListDeliveriesActivity.this,ShowMapsActivity.class);
        intent.putExtra("bundle", args);
        //startActivity(intent);
    }

} // end of ListDeliveriesActivity


And I thought I as doing so well.... o_O
 
Last edited:
Okay, this modification to code works.
I explicitly identified the column position rather than use the column name, which defeats my purpose of using cursor.getColumnIndexOrThrow("ColumnName").
IMHO using the explicit position of the column and passing that as a parameter is error prone.

Code:
        cursor = (Cursor) listDeliveriesView.getItemAtPosition(position);
        //alicplate = cursor.getString(cursor.getColumnIndexOrThrow("Licenseplate"));
        alicplate = cursor.getString(1);
        // location of depot
        //adeplat = cursor.getDouble(cursor.getColumnIndexOrThrow("DepLatitude"));
        adeplat = cursor.getDouble(2);
        //adeplng = cursor.getDouble(cursor.getColumnIndexOrThrow("DepLongitude"));
        adeplng = cursor.getDouble(3);
        depotLatLng = new LatLng(adeplat,adeplng);
        // location of pour site
        //asitelat = cursor.getDouble(cursor.getColumnIndexOrThrow("SiteLatitude"));
        asitelat = cursor.getDouble(4);
        //asitelng = cursor.getDouble(cursor.getColumnIndexOrThrow("SiteLongitude"));
        asitelng = cursor.getDouble(5);
        siteLatLng = new LatLng(asitelat,asitelng);
        // current location of truck
        //acurrlat = cursor.getDouble(cursor.getColumnIndexOrThrow("Latitude"));
        acurrlat = cursor.getDouble(6);
        //acurrlng = cursor.getDouble(cursor.getColumnIndexOrThrow("Longitude"));
        acurrlng = cursor.getDouble(7);
        currentLatLng = new LatLng(acurrlat, acurrlng);
        //
 
That's odd. Are you sure the cursor you're looking at is for the table you think it is? After all, you seem to have a list of cursors.
Best plan is to set a breakpoint at the point where the code is retrieving the column data, then you can examine the cursor values.
 
Yes I do have a few raw queries and related cursors and I am pretty sure as I have ran debug and checked the outputs. Time permitting, I'll work on this some more, right now I have a major issue that I hope you guys can help with some suggestions and pointers to literature and write-ups. It is so very difficult to get experienced coders...

In my geography, most locally affordable Android phones do not have a GPS chip. They use APIs from the local service provider, big competitors to Google. As I use Google Maps, I wonder what I need to import to enable my app to display Google maps?

Yes, I will start another thread.
 
But the error you are getting could not be clearer could it?

"column DepLatitude does not exist"

This is not an error with the Android library. It's an error in your code. You're doing something wrong, probably looking at the wrong cursor.
 
OK, I'll spend an hour on that and see what comes off it, there are a few queries and cursors there... thx.

YES, u r right. Typo error. "Deplatitude" instead of "DepLatitude" and everything falls into place.
Fresh pair of eyes in the morning
 
Last edited:
So a typo is something I checked for initially in your posted code, but everything seems to match up. I cannot find the word "Deplatitude" in the code you posted.
Good result in the end, but in future, please post correct code.
 
The code I posted was running a query on a table that I created in SQLite in a java helper class, which downloaded the data from mySQL. In the helper's table, it is "Deplatitude".
Sorry for the misunderstanding, there are a lot of moving parts here :D
 
Back
Top Bottom