昨天的问题今天发现是因为没有考虑到安卓中各个函数的生命周期所导致的,所以今天下午学习了一些相关的知识,也将代码改进了一下,将数据放到OnActivity之中,这样就可以令接收到的数据传递过去。
public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String i = editText.getText().toString(); Cursor cursor =mDatabaseHelper.selectlist(i); if (cursor != null){ while (cursor.moveToNext()){ CostBean costBean = new CostBean(); costBean.costTitle = cursor.getString(cursor.getColumnIndex("cost_title")); costBean.costDate = cursor.getString(cursor.getColumnIndex("cost_date")); costBean.costMoney = cursor.getString(cursor.getColumnIndex("cost_money")); mCostBeanList.add(costBean); Log.e("data",costBean.costMoney ); } cursor.close(); } generateValues(mCostBeanList); generateData(); SumList(); } }); }
private void SumList() { int sumzhichu = 0; int sumshouru = 0; int lastyue = 0; for (int i = 0; i < mCostBeanList.size(); i++) { CostBean costBean = mCostBeanList.get(i); int costMoney = Integer.parseInt(costBean.getCostMoney()); if(costMoney<=0){ sumzhichu += costMoney; }else{ sumshouru += costMoney; } } lastyue = sumzhichu+sumshouru; shouru.setText(String.valueOf(sumshouru)); zhichu.setText(String.valueOf(sumzhichu)); yue.setText(String.valueOf(lastyue));