Pulling data from Sqlite cursor loader

24 views Asked by At

OK I have a app that has a Listview and uses sqlite data to populate it. But I also want to be able to email the contents of each view for the body of the message. I tried doing this with a global string variable and catching the data in the CarCursorAdapter activity in the BindView section like this:

   // Update the TextViews with the attributes for the current move
       vinTxtView.setText(vinStr);
        dateTxtView.setText(dateStr);
        mvFromTxtView.setText(mvFrom);
        mvToTxtView.setText(mvTo);
        MyProperties.getInstance().bodyStrGlobal =   MyProperties.getInstance().bodyStrGlobal+vinStr+"-"+mvFrom+"->"+mvTo+"-"+dateStr+"\n";

And then I use that string in the email intent. But the problem is it keeps adding to this string every time the listview is populated so I get all kinds of double entries. What would be the best way to just capture this once when the email feature is selected? Or reset the string to null at some place? Maybe just read from each listview item instead of from the cursor loader? There is probably a way to just cycle through the database table but I'm getting all kinds of errors and haven't had any luck.

1

There are 1 answers

0
Greg Kraft On

Found this to work. My MainActivity is very busy now, but everything works.

 public String getEmailText(){
        String tempStr = "";
        String[] projection = {
                CarEntry._ID,
                CarEntry.COLUMN_CAR_VIN,
                CarEntry.COLUMN_CAR_DATE,
                CarEntry.COLUMN_CAR_MOVEFROM,
                CarEntry.COLUMN_CAR_MOVETO};

        Cursor  cursor = getContentResolver().query(Uri.parse("content://gregorykraft.com.scanvin/cars"),projection,null,null,null);

        if
        (cursor == null || cursor.getCount() < 1) {
            Toast.makeText(this, getString(R.string.error_uri),
                    Toast.LENGTH_SHORT).show();
            return "";
        }
        int i = 0;
        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                i++;
                String s =String.valueOf(i);
                String vinStr = cursor.getString(cursor.getColumnIndex(CarEntry.COLUMN_CAR_VIN));
                String mvFrom = cursor.getString(cursor.getColumnIndex(CarEntry.COLUMN_CAR_MOVEFROM));
                String mvTo = cursor.getString(cursor.getColumnIndex(CarEntry.COLUMN_CAR_MOVETO));
                String dateStr = cursor.getString(cursor.getColumnIndex(CarEntry.COLUMN_CAR_DATE));
                tempStr = tempStr+s+") "+vinStr + "-" + mvFrom + "->" + mvTo + "-" + dateStr + "\n";
                cursor.moveToNext();
            }
            cursor.close();
        }
        return tempStr;

    }