fooditem、foodtype、foodcost をデータベースに挿入するアプリケーションに取り組んでいます。
そして、BaseAdapter を使用して、これらすべてをリスト ビューで表示できます。ここまでは、問題なく動作しています。Activity クラスで宣言されている Textview に食費の合計を設定したいと考えています。しかし、私は 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;
}
}
前もって感謝します。