您的位置:首頁技術文章
文章詳情頁

android listview 每行的金額 求總和

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

問題描述

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

android listview 每行的金額 求總和

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

java代碼 已經測試過。 目前加減和一切正常。

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 值 加起來 ,然后再底部展示出總和? 我知道大概是用循環,但是實際操作完全沒有頭緒。 請大神們指點**

問題解答

回答1:

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

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

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

回答2:

用eventBus或者RxBus

回答3:

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

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

回答4:

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

相關文章:
国产综合久久一区二区三区