Java tutorial
/* * Copyright 2012 Mike Niyonkuru * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.niyonkuru.koodroid.ui; import android.content.Context; import android.database.Cursor; import android.os.Bundle; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.text.format.DateUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import android.widget.TextView; import net.niyonkuru.koodroid.R; import net.niyonkuru.koodroid.provider.AccountContract.TabTransactions; import net.niyonkuru.koodroid.provider.AccountContract.Tabs; /** * Displays {@link TabTransactions} for a specific subscriber. */ public class TabDetailFragment extends DetailFragment { private AlternatingColorAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle(null); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ListView lv = getListView(); View header = mContext.getLayoutInflater().inflate(R.layout.tab_list_item_labels, lv, false); lv.addHeaderView(header, null, false); lv.setDividerHeight(0); mAdapter = new TabTransactionAdapter(mContext); setListAdapter(mAdapter); setListShown(false); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { CursorLoader loader = new CursorLoader(mContext, Tabs.buildTransactionsUri(getSubscriber()), TabTransactionsQuery.PROJECTION, null, null, TabTransactions.DEFAULT_SORT); loader.setUpdateThrottle(DateUtils.SECOND_IN_MILLIS); return loader; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { mAdapter.swapCursor(data); if (isResumed()) { setListShown(true); } else { setListShownNoAnimation(true); } } @Override public void onLoaderReset(Loader<Cursor> loader) { mAdapter.swapCursor(null); } public class TabTransactionAdapter extends AlternatingColorAdapter { public TabTransactionAdapter(Context context) { super(context); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return LayoutInflater.from(context).inflate(R.layout.tab_list_item, parent, false); } @Override public void bindView(View view, Context context, Cursor cursor) { ViewHolder holder = (ViewHolder) view.getTag(); if (holder == null) { holder = new ViewHolder(); holder.reason = (TextView) view.findViewById(R.id.tab_transaction_reason); holder.date = (TextView) view.findViewById(R.id.tab_transaction_date); holder.amount = (TextView) view.findViewById(R.id.tab_transaction_amount); holder.balance = (TextView) view.findViewById(R.id.tab_transaction_balance); view.setTag(holder); } holder.reason.setText(cursor.getString(TabTransactionsQuery.REASON) + " " + cursor.getString(TabTransactionsQuery.TYPE)); holder.date.setText(cursor.getString(TabTransactionsQuery.DATE)); holder.amount.setText(formatMoney(cursor.getString(TabTransactionsQuery.AMOUNT))); holder.balance.setText(formatMoney(cursor.getString(TabTransactionsQuery.BALANCE))); } class ViewHolder { TextView reason, date, amount, balance; } } /** * {@link TabTransactions} query parameters */ public interface TabTransactionsQuery { String[] PROJECTION = { TabTransactions._ID, TabTransactions.TAB_TRANSACTION_DATE, TabTransactions.TAB_TRANSACTION_REASON, TabTransactions.TAB_TRANSACTION_TYPE, TabTransactions.TAB_TRANSACTION_AMOUNT, TabTransactions.TAB_TRANSACTION_BALANCE }; int DATE = 1; int REASON = 2; int TYPE = 3; int AMOUNT = 4; int BALANCE = 5; } }