jeudi 5 mars 2015

How set TextView value with sum of numeric ListView loaded from database



I have a Fragment composed by a ListView and a TextView. ListView contains several TextView with numeric values loaded from database via custom SimpleCursorAdapter and CursorLoader. So these values are static after displayed. I want to sum values and show directly the total in the separate TextView. How can i do it? I mean from user point of view the list and the total must be displayed at the same time. I can use an additional loader for getting the total separately but I want to avoid this. So in which point should I put sum logic?


Already checked other questions but I didn't find a solution for my case.


Here it is my code:


FRAGMENT onCreateView



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


FRAGMENT onActivityCreated



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


FRAGMENT onLoaderFinished



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


ADAPTER bindView (Adapter 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);


}


FRAGMENT LAYOUT montly_report.xml



<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
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>



Aucun commentaire:

Enregistrer un commentaire