android - 在 ListView 中显示之前合并游标中的数据

标签 android listview android-sqlite android-cursoradapter android-cursorloader

我正在开发一个应用程序,但目前卡住了。我要做的是在将数据显示在 UI 上之前处理来自数据库的数据。

我的应用程序像这样收集数据并存储在数据库中。

+--------------------+
| 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);
}

适配器代码。

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));           
}

我还想创建一个新表、sharedpreference 或 hashmap 来存储聚合数据,但我认为这些都是不必要的开销,因为应用程序会不断收集数据,并且有一种方法可以通过处理适配器来解决这个问题。

有人可以建议一种有效的方法吗?提前谢谢你。

最佳答案

听起来您正在寻找以下查询,但由于 CursorLoader 没有为您提供执行聚合函数的选项,您被卡住了:

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

您应该阅读这篇关于创建自定义 URI 并可能使用 SQLiteQueryBuilder 的文章在您的 ContentProvider 中,以便使用 CursorLoader 执行此查询

GROUP BY with CursorLoader

关于android - 在 ListView 中显示之前合并游标中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20336217/

相关文章:

android - 具有单选模式的 ListView 希望自动选择第一项并调用其 onitemclicklistener

android - 在 ScrollView 中添加 ListView

android - 禁用 ContentProvider URI 上的通知

java - SQLite DatabaseHelper 类不创建数据库

android - 安全异常 : Permission denied (missing INTERNET permission? )

android - 为 ACTION_SEND Intent 过滤器 uri 获取持久性 URI 权限导致 SecurityException

android - WebKit/WebView tap/onclick 默认高亮(Android Ice Cream Sandwich)

android - 在 Android 上使用 Espresso 测试 EditText View 是否没有设置错误文本?

java - 如何在 SQLite 中向表添加外键?