2012-08-24 66 views
0

我正在處理將食物資料,foodtype和foodcost插入數據庫的應用程序。如何獲得從BaseAdapter類到活動調用的浮動值BaseAdapter

而且我可以使用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; 
    } 

}

在此先感謝。

回答

0

也許你會在適配器類中添加一個getArray()方法來從Adapter中獲取數據,並計算Activity中的數據。

+0

我已經使用公衆持股量getSunTotal()方法來獲得的總成本,但它返回0.0。 – Rajendra

+0

解決了now.just在adapterClass Constructor.and中傳遞了textView,並在adapterClass中將此foodcost設置爲此textview。 – Rajendra