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

GridView item list selector for multiple items not working in Android
onRegistered() from GCMIntentService never called
Read informations from application preferences instead from AndroidManifest.xml
Work with InputTextBox in corona under windows environment
How to drawing on surface view canvas in android
Recycle bitmaps on a listview causes issues
How to have tabs at the bottom while using action bar sherlock?
ICS TimePicker sliders has weird see-thru styles
SpeechRecognizer not detecting end of speech on jelly bean
Mylocation not works in Galaxy tab p1300
How to kill background music.
how to create pdf file with multiple pages from image file in android
Incoming call broadcast receiver not working (Android 4.1)
Exception on JSON parsing
Parsed XML data not retrieving in android [duplicate]
How to profile the App uses in Android?

Categories

HOME
embedded
kentico
scikit-learn
apache-jena
case
powerpoint
obfuscation
alarm
ms-access-2016
google-cloud-functions
iis-6
azure-container-service
crystal-lang
qpython
g++
github-enterprise
mod-security
mautic
emoji
datastage
plink
screen-readers
powershell-v5.0
mvc-mini-profiler
pyramid
strongname
beautifier
pylons
ms-access-web-app
asymptotic-complexity
send
recurring-events
scatter3d
weather
clickjacking
gnat-gps
vue-chartjs
dynamic-jasper
hunspell
jwplayer6
searchkit
protege4
openerp-7
tasm
softmax
vaadin-charts
httr
double-click
lampp
cnc
spring-integration-sftp
netbeans6.8
mongotemplate
context-switch
mach-o
mouseleave
pybrain
gameplay-kit
django-1.10
filenet
winusb
webjars
pacemaker
simevents
dbamp
nullable
gocql
dllimport
lazybones
google-books
typemock
nutiteq
jbake
jquery-slider
terminate
decoupling
websphere-esb
away3d
cffile
octal
android-icons
chart-director
bjyauthorize
monotouch.dialog
vfs
thttpd
jquery-address
ikimagebrowserview
android-holo-everywhere
u2netdk
entitydatasource
clrstoredprocedure
xui
twitterizer
folding
murmurhash
data-mapping
sun
escrow
lemmatization
movieplayer

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