Home | History | Annotate | Download | only in calculator2
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.calculator2;
     18 
     19 import android.view.LayoutInflater;
     20 import android.view.ViewGroup;
     21 import android.view.View;
     22 import android.content.Context;
     23 import android.widget.BaseAdapter;
     24 import android.widget.TextView;
     25 
     26 import java.util.Vector;
     27 
     28 import org.javia.arity.SyntaxException;
     29 
     30 class HistoryAdapter extends BaseAdapter {
     31     private Vector<HistoryEntry> mEntries;
     32     private LayoutInflater mInflater;
     33     private Logic mEval;
     34 
     35     HistoryAdapter(Context context, History history, Logic evaluator) {
     36         mEntries = history.mEntries;
     37         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     38         mEval = evaluator;
     39     }
     40 
     41     // @Override
     42     public int getCount() {
     43         return mEntries.size() - 1;
     44     }
     45 
     46     // @Override
     47     public Object getItem(int position) {
     48         return mEntries.elementAt(position);
     49     }
     50 
     51     // @Override
     52     public long getItemId(int position) {
     53         return position;
     54     }
     55 
     56     @Override
     57     public boolean hasStableIds() {
     58         return true;
     59     }
     60 
     61     // @Override
     62     public View getView(int position, View convertView, ViewGroup parent) {
     63         View view;
     64         if (convertView == null) {
     65             view = mInflater.inflate(R.layout.history_item, parent, false);
     66         } else {
     67             view = convertView;
     68         }
     69 
     70         TextView expr   = (TextView) view.findViewById(R.id.historyExpr);
     71         TextView result = (TextView) view.findViewById(R.id.historyResult);
     72 
     73         HistoryEntry entry = mEntries.elementAt(position);
     74         String base = entry.getBase();
     75         expr.setText(entry.getBase());
     76 
     77         try {
     78             String res = mEval.evaluate(base);
     79             result.setText("= " + res);
     80         } catch (SyntaxException e) {
     81             result.setText("");
     82         }
     83 
     84         return view;
     85     }
     86 }
     87 
     88