【问题标题】:How set TextView value with sum of numeric ListView loaded from database如何使用从数据库加载的数字 ListView 的总和设置 TextView 值
【发布时间】:2015-03-05 13:20:50
【问题描述】:

我有一个由ListViewTextView 组成的Fragment。 ListView 包含几个TextView,通过自定义SimpleCursorAdapterCursorLoader 从数据库加载数值。所以这些值在显示后是静态的。我想对值求和并在单独的 TextView 中直接显示总数。 我该怎么做?我的意思是从用户的角度来看,列表和总数必须同时显示。我可以使用额外的加载器单独获取总数,但我想避免这种情况。那么我应该把求和逻辑放在哪一点呢?

我有一个这样的查询,其中每一行都会为 listView 填充一个项目:

SELECT group_name, group_amount FROM notes;

我不想执行如下单独的查询来计算总量,如果可能的话,我想从 GUI 中安全地获取它

SELECT sum(group_amount) FROM notes;

这是我的代码:

创建视图上的片段

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.monthly_report, container, false);
        ListView listCosts = (ListView)rootView.findViewById(R.id.list_costs);
        costsAdapter = new VehReportAdapter(getActivity(), 
                    com.vortexalex.vehnotes.R.layout.monthly_report_single_item, null,
                    VehReportAdapter.fromColumns, toViews, 0);
        listCosts.setAdapter(costsAdapter);
}

片段 onActivityCreated

public void onActivityCreated(Bundle savedInstanceState) {
        getLoaderManager().initLoader(VehTabsUtil.REPORT_COSTS_LOADER_ID, null, (LoaderCallbacks<Cursor>) this); 
}

加载器已完成的片段

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

适配器绑定视图(适配器extends SimpleCursorAdapter

public void bindView(View view, Context context, Cursor cursor) {
    Integer dbAmount = cursor.getInt(cursor.getColumnIndex(AccountingContract.Report.COLUMN_NAME_GROUP_AMOUNT));
    String amount = NumberUtil.formatAmount(dbAmount);
    ((TextView) view.findViewById(com.vortexalex.vehnotes.R.id.report_item_amount)).setText(amount);        

}

片段布局 montly_report.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:orientation="vertical"
    android:padding="6dip" >

    <ListView android:id="@+id/list_costs"
               android:layout_width="wrap_content"
               android:layout_height="0dip"
               android:layout_weight="2"
               android:drawSelectorOnTop="false"/>

    <TextView 
        android:id="@+id/costs_sum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:padding="6dip"
     />
</LinearLayout>

【问题讨论】:

  • 什么值?列表中的项目总数?
  • @Apurva 不,它们代表金额
  • 我用关于 SQL 查询的新细节编辑了我的帖子
  • 同时发布您的数据库助手代码
  • 我不认为发布简单的帮助代码很有用。 “查询”部分运行良好。我刚刚实现了Loader.loadOnBackground 方法,它调用了助手并返回了Cursor

标签: java android android-listview simplecursoradapter


【解决方案1】:

如果我理解正确,您的列表视图中有数字,并且您想要列表视图中每个值的总和。

要对列表视图中的每个值实现此循环,请使用Integer.parseInt(String str) 将值从String 转换为int,将它们全部相加,然后使用@ 将生成的int 转换回String 987654326@,然后使用 yourTextView.setText(resultString) 在文本视图中插入生成的 String

【讨论】:

  • 类型转换只占很小的一部分。我如何迭代列表视图?最重要的是 WHERE... 在哪个组件中?适配器还是片段?考虑到总和之后我必须能够访问总和 TextView
  • 遍历列表视图非常简单,只需遍历列表视图的适配器即可。把它放在哪里取决于你,因为我不知道你什么时候想显示值。可以将 textview 做成一个字段以便于参考。
  • 不过,我可以想到几个合适的地方进行迭代。如果您希望每次活动开始时刷新值,您可以刷新活动 onStart() 中的值。您还可以覆盖notifyDataSetChanged(),以在列表视图中的值更改时刷新总和(提示,提示,我会使用第二个选项)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-25
  • 2013-09-18
  • 2011-10-19
  • 2018-08-15
  • 1970-01-01
  • 1970-01-01
  • 2016-02-14
相关资源
最近更新 更多