android


Display the total amount from TextView values in a Custom Listview


I have a shopping cart in my app, and I have been wondering how would I be able to get the sum of the values from the textviews in my listview, and then display it in another class?
That orderTotal array is the amount of quantity of products in each row
This is My Adapter Class:
public class ListCartAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> orderTotal;
public ListCartAdapter(Context context, ArrayList<String> orderTotal){
this.context = context;
this.orderTotal = orderTotal;
}
#Override
public int getCount() {
return orderName.size();
}
#Override
public Object getItem(int position) {
return orderName.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final cartDatabaseHelper db = new cartDatabaseHelper(context);
final View listView;
final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listView = inflater.inflate(R.layout.cart_list_item, null);
TextView total = (TextView) listView.findViewById(R.id.textOrderTotal);
total.setText(orderTotal.get(position));
return listView;
}
And this is MainActivity class:
//CART LISTVIEW
private ArrayList<String> orderid;
private ArrayList<String> orderName;
private ArrayList<String> orderSize;
private ArrayList<String> orderQuantity;
private ArrayList<String> orderTotal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
//CART LISTVIEW
orderid = new ArrayList<>();
orderName = new ArrayList<>();
orderSize = new ArrayList<>();
orderQuantity = new ArrayList<>();
orderTotal = new ArrayList<>();
TextView textTotal = (TextView) findViewById(R.id.textOrderSumTotal);
ListView listView = (ListView) findViewById(R.id.listView);
ListCartAdapter adapter = new ListCartAdapter(cart.this, orderid, orderName, orderSize, orderQuantity, orderTotal);
listView.setAdapter(adapter);
Cursor data = db.getListContents();
if(data.getCount() == 0){
btnCheckout.setVisibility(View.GONE);
}
else
{
btnCheckout.setVisibility(View.VISIBLE);
data.moveToFirst();
do{
orderid.add(data.getString(0));
orderName.add(data.getString(1));
orderSize.add(data.getString(2));
orderQuantity.add(data.getString(3));
orderTotal.add(data.getString(4));
} while (data.moveToNext());
}
data.close();
listView.setEmptyView(findViewById(R.id.emptyView));
}
The textTotal is where I am planning to display the total of the values from the listview.
Can anybody point me to the right direction? Thanks in advance!
Its simple if you've all prices listed in your array list named orderTotal. Iterate over the list and add all the values as following:
int total = 0;
for(String s : orderTotal){
total += Integer.parseInt(s);
}
display this total wherever you want. If price is float, then use Float.parseFloat(YOUR_FLOAT_STRING);

Related Links

ImageButton background not changing
Triggering An Event At Certain Dates Android
Error:Conflict with dependency 'com.android.support: when update from 23.1.1 to 23.4.0
How to hide navigation drawer when opening certain fragment?
INSTALL_REFERRER intent gets fired continuously with some random UTM parameters
CSV file not uploading in android application from any file manager in motorola phone but works fine in lava
android - Error in passing string fragment to fragment
How to calibrate an external microphone device?
Is it possible to send HashMap as extra in adb shell am broadcast?
How to set value in ArrayList inside ArrayList?
Google People Api Implementation in android application
CWAC cam2 version 4.0 orientation issue
CheckBox checked on scroll automatically in ListView in android
how to create SQLite database in external folder like Whats app does in Android?
Making form on android studio
SQLite database : data folder persmission

Categories

HOME
embedded
porting
awk
string
webdriver
gpu
zend-framework
mysql-workbench
gradient
grafana
bitbucket-api
telegram-bot
regular-language
bookmarks
jetty
startup
mautic
datastage
pybossa
atlassian
android-intent
libusb
package-managers
gruntfile
event-sourcing
entitymanager
mvc-mini-profiler
imessage
nodemcu
boost-icl
selinux
windows-xp
ag
jquery-ias
question2answer
named-entity-recognition
ios10.2
angular-routing
pimcore
zxing
gmock
google-api-client
xcode-ui-testing
interbase
rpgle
paperjs
quantlib
openmdao
boost-asio
aws-codecommit
jett
opensmpp
visa
stringtokenizer
mschart
twilio-click-to-call
aquamacs
css-transforms
monkey
gpg-signature
cd-burning
renaming
restore
cubism.js
custom-lists
installshield-2010
tracker-enabled-dbcontext
query-by-example
aurora
cortex-m
grass
concept-insights
sciruby
themoviedb-api
zen-cart
extjs-grid
avahi
typeconverter
zend-currency
android-broadcast
ghcjs
crowd
metatrader5
iso-prolog
mobility
parallel-testing
jjaql
winginx
multiautocompletetextview
drupal-nodes
unit-of-work
process-explorer
kaleidoscope
octal
actiondispatch
pdfviewer
web-farm
role
jqote
mtu
undefined-index
load-time
binomial-heap
murmurhash
fail-fast-fail-early
software-lifecycle
fault-tolerance
fdf
wcf-proxy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App