Question

I'm developing an app and currently stuck. What I'm gonna do is dealing with data from DB before showing it on the UI.

My app collects data and store in DB like this.

+--------------------+
| Name  Used_time(s) |
+--------------------+
| C       10         |
| B       12         |
| A       23         |
| C       11         |
| A       14         |
+--------------------+

I implemented a CursorLoader and a custom CursorAdapter in basic way, and it's showing my raw DB data and its updates correctly! Good for me!

But what I want to do is showing aggregated time for an app in the listview like this.

+--------------------+
| Name  Used_time(s) |
+--------------------+
| C       21         |
| B       12         |
| A       37         |
+--------------------+

I have tried to modify my cursor adapter to do summation in the adapter, but it automatically gets cursor position for each cursor, and return a view on that position of listview whether I touch the Used_time data or not. I couldn't find a workaround.

My current loader code in the activity class..

private void loadDB() {
    getLoaderManager().initLoader(0, null, this);   
    cursorAdapter = new MyCursorAdapter(this, null);
    setListAdapter(cursorAdapter);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = MyTable.MY_COLUMNS ;
    CursorLoader cursorLoader= new CursorLoader(this, MyContentProvider.CONTENT_URI_APPS, projection, null, null, null);
    return cursorLoader;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    cursorAdapter.swapCursor(cursor);
}

Adapter code.

public MyCursorAdapter(Context context, Cursor cursor) {
    super(context, cursor);
        LayoutInflater inflater = LayoutInflater.from(context);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return inflater.inflate(R.layout.app_row, parent, false);
}

@Override
public void bindView (View view, Context context, Cursor cursor) {
    TextView NameTv = (TextView)view.findViewById(R.id.name);
    TextView TimeTv = (TextView)view.findViewById(R.id.time);

    String Name = cursor.getString(1);
    appNameTv.setText(Name);

    int Time = (int)(cursor.getLong(2));
    TimeTv.setText(String.valueOf(Time));           
}

I also thought to make a new table, sharedpreference or hashmap to store aggregated data but I think those are unnecessary overhead since the app keep collect data and there would be a way to solve this with dealing with adapter.

Could anybody suggest an efficient way to do this? Thank you in advance.

Was it helpful?

Solution

Sounds like the following query is what you're looking for, but you're stuck since CursorLoader doesn't give you the option to do aggregate functions:

SELECT Name, COUNT(Used_time) FROM MyTable GROUP BY Name;

You should read this article about creating custom URIs and possibly using SQLiteQueryBuilder in your ContentProvider in order to use the CursorLoader to do this query

GROUP BY with CursorLoader

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