0

我正在开发一个应用程序,我将 fooditem、foodtype 和 foodcost 插入数据库。

而且我可以使用 BaseAdapter 在列表视图中显示所有这些。到目前为止,它工作正常。我想在我的 Activity 类中声明的 Textview 中设置 foodcost 的总和。但我得到的食品成本总和等于 0.0。我在 BaseAdapter 类中计算。

我的 Activity 和 BaseAdapter 代码如图所示。

        public class ViewList extends Activity {
        private List<DataArray> list; 
    Context mContext;
public TextView sum;
private ViewAdapter mViewAdapter;

    @Override
     protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
     mContext= this.getApplicationContext();
     Intent mIntent= getIntent();
     int DMY=(int) mIntent.getIntExtra("DMY",1111);
     String TYPE=mIntent.getStringExtra("TYPE");
     switch(DMY){
     case 000:
         list=makeQuery(null,null,null,TYPE,DMY);
         break;
     case 001:
         String YEAR=mIntent.getStringExtra("YEAR");
         list=makeQuery(null,null,YEAR,TYPE,DMY);
         break;
    case  011:
         String YEAR1=mIntent.getStringExtra("YEAR");
         String MONTH=mIntent.getStringExtra("MONTH");
         list=makeQuery(null,MONTH,YEAR1,TYPE,DMY);
        break;
    case 111:
         String YEAR2=mIntent.getStringExtra("YEAR");
         String MONTH1=mIntent.getStringExtra("MONTH");
         String DATE=mIntent.getStringExtra("DATE");
         list=makeQuery(DATE,MONTH1,YEAR2,TYPE,DMY);
        break;
        default:
    }

    if(!list.isEmpty()){
        setContentView(R.layout.view_list);
        ListView mListViw = (ListView) findViewById(R.id.listView_expenditure);
        sum= (TextView)findViewById(R.id.textView_sum);
        mViewAdapter= new ViewAdapter(ViewList.this,list);
        mListViw.setAdapter(mViewAdapter);
       // I am calling getsumTotal() method to get sum of foodcost. but its return 0.0
                    sum.setText(String.valueOf(mViewAdapter.getSunTotal()));


    }else{
        ShowDialog("No record(s) found in Database");
    }

}

BaseAdapter 类。

public class ViewAdapter extends BaseAdapter {
    public  float sunTotal=0;
    private static LayoutInflater mInflater;

    private List<DataArray> list;
    private DataArray myDataArray;
     private Activity mActivity;
    public ViewAdapter(Activity mActivity, List<DataArray> list) {
    // TODO Auto-generated constructor stub
    this.list=list;
    mInflater=(LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

    @Override
    public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
    }

    @Override
    public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
    }

    @Override
     public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
     }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
     // TODO Auto-generated method stub
     View mView=convertView;
     if(convertView==null)
     mView=mInflater.inflate(R.layout.view_helper,parent,false);
     myDataArray=list.get(position);    
     TextView itemName = (TextView) mView.findViewById(R.id.textView_item_name);
     itemName.setText(myDataArray.getItem());
     TextView itemprice = (TextView) mView.findViewById(R.id.textView_price);
     String price=myDataArray.getCost();
      itemprice.setText(price);
      float mFloat = Float.parseFloat(price);
      sunTotal=sunTotal+mFloat;// calculating sum  of foodcost.
       Log.d("Sum",""+sunTotal);
      TextView itemDate = (TextView) mView.findViewById(R.id.textView_date);
       String date=myDataArray.getDate()+" "+myDataArray.getMonth();
      itemDate.setText(date);
       return mView ;
   }
      // get method to get the total of foodcost.which i have used in activity class to get the total foodcost
    public float getSunTotal() {
    return sunTotal;
   }

}

提前致谢。

4

2 回答 2

0

或许你会在Adapter Class中添加一个getArray()方法从Adapter中获取Data,并在Activity中计算数据。

于 2012-08-24T17:00:08.557 回答
0

您可以使用 将LocalBroadcastManager事件从您的适配器广播到您的活动:

http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

于 2012-08-24T17:04:15.200 回答