av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

android listview 每行的金額 求總和

瀏覽:211日期:2024-08-30 08:43:46

問題描述

我用listview 把一些數(shù)據(jù)通過simpleAdapter 展示出來。 詳情如截圖

android listview 每行的金額 求總和

加減 按鈕是修改數(shù)量。 與此同時,右邊的 和 也會隨著數(shù)量的更改而 更新。

java代碼 已經(jīng)測試過。 目前加減和一切正常。

int cal_quantity;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main8); List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>(); final String name[]={'apple','orange','pear'}; final String quantity[]={'1','2','3'}; final String price[]={'5','10','2'}; for(int i=0;i<name.length;i++){HashMap<String, String> map = new HashMap<>();map.put('name',name[i]);map.put('quantity',quantity[i]);map.put('price',price[i]);aList.add(map); } String[] from = {'name','quantity','price'}; int[] to = {R.id.name,R.id.quantity,R.id.price}; SimpleAdapter adapter = new SimpleAdapter(this, aList, R.layout.main7, from, to){@Overridepublic View getView(final int position, View convertView, ViewGroup parent) { View v = super.getView(position, convertView, parent); final TextView tv_quantity=(TextView)v.findViewById(R.id.quantity); final TextView tv_price=(TextView)v.findViewById(R.id.price); final TextView tv_total=(TextView)v.findViewById(R.id.total); final int get_quantity = Integer.parseInt(tv_quantity.getText().toString()); final double get_price= Double.parseDouble(tv_price.getText().toString()); final double get_total=get_quantity*get_price; tv_total.setText(get_total+''); Button minus=(Button)v.findViewById(R.id.minus); minus.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) { cal_quantity=Integer.parseInt(tv_quantity.getText().toString()); if(cal_quantity!=1){cal_quantity=cal_quantity-1; } tv_quantity.setText(cal_quantity+''); double get_total=cal_quantity*get_price; tv_total.setText(get_total+'');} }); Button plus=(Button)v.findViewById(R.id.plus); plus.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) { cal_quantity=Integer.parseInt(tv_quantity.getText().toString()); cal_quantity=cal_quantity+1; tv_quantity.setText(cal_quantity+''); double get_total=cal_quantity*get_price; tv_total.setText(get_total+'');} }); return v;} }; ListView listView = (ListView) findViewById(R.id.listview); listView.setAdapter(adapter);}

xml - listview和底部的總和 textview分開

<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'xmlns:tools='http://schemas.android.com/tools'android:id='@+id/activity_main8'android:layout_width='match_parent'android:layout_height='match_parent'android:paddingBottom='@dimen/activity_vertical_margin'android:paddingLeft='@dimen/activity_horizontal_margin'android:paddingRight='@dimen/activity_horizontal_margin'android:paddingTop='@dimen/activity_vertical_margin'tools:context='com.yu.singleton.Main8Activity'android:orientation='vertical'><LinearLayout android:orientation='vertical' android:layout_width='match_parent' android:layout_weight='0.3' android:layout_height='match_parent'> <ListViewandroid:layout_width='match_parent'android:layout_height='match_parent'android: /></LinearLayout><LinearLayout android:orientation='vertical' android:layout_width='match_parent' android:background='@android:color/holo_blue_dark' android:layout_weight='0.7' android:layout_height='match_parent'> <TextViewandroid:text='Total'android:layout_width='match_parent'android:layout_height='wrap_content'android: android:textAlignment='center'android:textSize='36sp' /></LinearLayout></LinearLayout>

**那么我的問題是如何把每一排最右邊的 Textview 值 加起來 ,然后再底部展示出總和? 我知道大概是用循環(huán),但是實際操作完全沒有頭緒。 請大神們指點**

問題解答

回答1:

我補充下 @mw2972 的回答.因為總價只是跟初始數(shù)據(jù)和每一次的操作有關(guān), 所以可以在Activity中維護一個總價變量totalPrice, 然后初始化列表的時候給它賦值, 在每次點擊加減操作的時候修改它的值就可以一直知道當前列表的總價了.

初始化的時機: 什么時候你把列表數(shù)據(jù)賦值給adapter就什么時候遍歷一次數(shù)據(jù)計算總價就行了.修改時機: 點擊加減操作的時候修改, 同時更新總價TextView的顯示內(nèi)容就可以了.

最后, 把Adapter放在Activity其實是一個不好的習慣.

回答2:

用eventBus或者RxBus

回答3:

數(shù)據(jù)和顯示最好分離開來,不要依賴界面上的文本作為數(shù)據(jù)來源。包括點擊加減按鈕,都應該是是修改quantity數(shù)組的值,然后再更新界面

總和的話,就是每行的價格x數(shù)量,然后相加,然后更新下面的文本

回答4:

首先,把quantity,price和name三個數(shù)組改成Activity的成員變量,這樣確保不同的view都可以訪問它們。然后,修改各個onClick方法。因為現(xiàn)在可以訪問到所有物品的數(shù)量和單價,總價格就不難計算了。

相關(guān)文章:
主站蜘蛛池模板: 超碰人人插 | 日韩一区二区三区在线播放 | 久久香蕉精品 | 亚洲亚洲人成综合网络 | 亚洲网在线 | 久久天天躁狠狠躁夜夜躁2014 | 亚洲91精品 | 毛片网站免费观看 | 三级黄色片网站 | 欧美日韩在线免费观看 | 成人国产网站 | 免费a视频| 亚洲一区二区三区在线视频 | 午夜影院黄 | 久久午夜视频 | 六月色婷婷 | 欧美久久久 | 一级片黄色片 | 亚洲 欧美 日韩 在线 | 91精品国产日韩91久久久久久 | 久久观看 | 在线观看av的网站 | 亚洲综人网 | 国产一区在线播放 | 999精品在线 | 国产高清在线观看 | 国产成人一区二区 | 欧美三级又粗又硬 | 一级黄色在线观看 | 黄色成年人网站 | 亚洲一区二区三区 | 欧美日韩国产片 | www一级片 | 91亚洲国产成人精品性色 | 五月婷婷综合网 | 午夜激情网 | 毛片在线观看网站 | 免费一级黄色 | 亚洲精品一区二三区不卡 | 欧美日韩国产成人 | 一区二区免费看 |