如何从BaseAdapter类获取浮点值到调用BaseAdapter的活动

时间:2012-08-24 15:51:18

标签: android

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

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

我的活动代码和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;
   }

}

提前致谢。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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