dimanche 3 avril 2016

How to display sqlite row values into columns in Android

I have a table with the following

         Event   TRIES
Contact1 A       1/2 
Contact2 B       2/4
Contact2 A       1/4
Contact1 C       1/2
Contact1 B       1/3

And I want my data to be displayed as:

            A            B          C
 Contact1  sum of As   sum of Bs   Sum of Cs
 Contact2  sum of As   sum of Bs   Sum of Cs

I use the following to get them in a map

crs.moveToFirst();
    while (crs.moveToNext())
    {
        String Mykeyvalue = crs.getString(crs.getColumnIndex("ContactName"));
        if (result.containsKey(Mykeyvalue))
        {
            String event = crs.getString(crs.getColumnIndex("s_event"));

            double ats = crs.getDouble(crs.getColumnIndex("Ats"));
            int points = crs.getInt(crs.getColumnIndex("Score"));

            String total = String.valueOf(ats)+"/"+String.valueOf(points);

            result.get(ContactName).put(event, total);
        } else
        {
            result.put(ContactName, new LinkedHashMap<String, String>());

            String event = crs.getString(crs.getColumnIndex("s_event"));

            double ats = crs.getDouble(crs.getColumnIndex("Ats"));
            int points = crs.getInt(crs.getColumnIndex("Score"));

            String total = String.valueOf(ats)+"/"+String.valueOf(points);

            result.get(ContactName).put(event, total);
        }
    }
    return result;

But it skips records for example I get only Contact1 sum of Bs

Is there a simpler way?

Aucun commentaire:

Enregistrer un commentaire