android listview 每行的金額 求總和
問(wèn)題描述
我用listview 把一些數(shù)據(jù)通過(guò)simpleAdapter 展示出來(lái)。 詳情如截圖
加減 按鈕是修改數(shù)量。 與此同時(shí),右邊的 和 也會(huì)隨著數(shù)量的更改而 更新。
java代碼 已經(jīng)測(cè)試過(guò)。 目前加減和一切正常。
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分開(kāi)
<?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>
**那么我的問(wèn)題是如何把每一排最右邊的 Textview 值 加起來(lái) ,然后再底部展示出總和? 我知道大概是用循環(huán),但是實(shí)際操作完全沒(méi)有頭緒。 請(qǐng)大神們指點(diǎn)**
問(wèn)題解答
回答1:我補(bǔ)充下 @mw2972 的回答.因?yàn)榭們r(jià)只是跟初始數(shù)據(jù)和每一次的操作有關(guān), 所以可以在Activity中維護(hù)一個(gè)總價(jià)變量totalPrice, 然后初始化列表的時(shí)候給它賦值, 在每次點(diǎn)擊加減操作的時(shí)候修改它的值就可以一直知道當(dāng)前列表的總價(jià)了.
初始化的時(shí)機(jī): 什么時(shí)候你把列表數(shù)據(jù)賦值給adapter就什么時(shí)候遍歷一次數(shù)據(jù)計(jì)算總價(jià)就行了.修改時(shí)機(jī): 點(diǎn)擊加減操作的時(shí)候修改, 同時(shí)更新總價(jià)TextView的顯示內(nèi)容就可以了.
最后, 把Adapter放在Activity其實(shí)是一個(gè)不好的習(xí)慣.
回答2:用eventBus或者RxBus
回答3:數(shù)據(jù)和顯示最好分離開(kāi)來(lái),不要依賴界面上的文本作為數(shù)據(jù)來(lái)源。包括點(diǎn)擊加減按鈕,都應(yīng)該是是修改quantity數(shù)組的值,然后再更新界面
總和的話,就是每行的價(jià)格x數(shù)量,然后相加,然后更新下面的文本
回答4:首先,把quantity,price和name三個(gè)數(shù)組改成Activity的成員變量,這樣確保不同的view都可以訪問(wèn)它們。然后,修改各個(gè)onClick方法。因?yàn)楝F(xiàn)在可以訪問(wèn)到所有物品的數(shù)量和單價(jià),總價(jià)格就不難計(jì)算了。
相關(guān)文章:
1. 前端 - 應(yīng)該先從angularJS , vue.js , react 這些框架中的哪個(gè)開(kāi)始入手?2. 用CSS3 box-sizing 屬性實(shí)現(xiàn)兩個(gè)并排的容器,如果想讓容器中間有間隔該如何實(shí)現(xiàn)3. 如何使api.douban.com域名可用,調(diào)用該域名時(shí)微信官方判斷該域名不被接受4. 域名解析在Ubuntu64 9.04計(jì)算機(jī)上的Java應(yīng)用程序中不起作用所有其他軟件可以正確解析DNS5. android - weex 項(xiàng)目createInstanceReferenceError: Vue is not defined6. javascript - 如圖,百度首頁(yè),查看源代碼為什么什么都沒(méi)有?7. pdo 寫(xiě)入到數(shù)據(jù)庫(kù)的內(nèi)容為中文的時(shí)候?qū)懭雭y碼8. PHP類封裝的插入數(shù)據(jù),總是插入不成功,返回false;9. android - 哪位大神知道java后臺(tái)的api接口的對(duì)象傳到前端后輸入日期報(bào)錯(cuò),是什么情況?求大神指點(diǎn)10. 怎么在phpstudy中用phpexcel上傳數(shù)據(jù)到MYSQL?
