BudgetAdapter extends ArrayAdapter<Budget> { @Override public View getView(int position, View convertView, ViewGroup parent) { Budget item = getItem(position); ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.budget_layout, parent, false); holder = new ViewHolder(); holder.budgetName = (TextView) convertView.findViewById(R.id.budgetName); holder.budgetBar = (ProgressBar) convertView.findViewById(R.id.budgetBar); holder.budgetValue = (TextView) convertView.findViewById(R.id.budgetValue); convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } holder.budgetName.setText(item.name); holder.budgetBar.setMax(item.max); holder.budgetBar.setProgress(item.current); String fraction = String.format(FRACTION_FORMAT, item.current, item.max); holder.budgetValue.setText(fraction); return convertView; } BudgetAdapter(Context context, List<Budget> items); @Override View getView(int position, View convertView, ViewGroup parent);  }
@Test public void TestAdapter() { ActivityController activityController = Robolectric.buildActivity(BudgetActivity.class).create(); Activity activity = (Activity)activityController.get(); DBHelper db = new DBHelper(activity); final String NAME = "name"; final int BUDGET = 100; final double CURRENT = 50.01; final int CURRENT_CEIL = (int)Math.ceil(CURRENT); db.insertBudget(NAME, BUDGET); db.insertTransaction(DBHelper.TransactionDbIds.EXPENSE, "", "", NAME, CURRENT, "", nowMs, ""); db.close(); final int SCALED_BUDGET = BUDGET * (MONTHS_PER_YEAR+1); final List<Budget> budgets = db.getBudgets(lastYearMs, nowMs); final BudgetAdapter adapter = new BudgetAdapter(activity, budgets); View view = adapter.getView(0, null, null); final TextView budgetName = (TextView) view.findViewById(R.id.budgetName); final ProgressBar budgetBar = (ProgressBar) view.findViewById(R.id.budgetBar); final TextView budgetValue = (TextView) view.findViewById(R.id.budgetValue); assertEquals(NAME, budgetName.getText().toString()); assertEquals(CURRENT_CEIL, budgetBar.getProgress()); assertEquals(SCALED_BUDGET, budgetBar.getMax()); String fractionFormat = activity.getResources().getString(R.string.fraction); String fraction = String.format(fractionFormat, CURRENT_CEIL, SCALED_BUDGET); assertEquals(fraction, budgetValue.getText().toString()); }