Java tutorial
/** * Copyright (C) 2012 Picon software * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package fr.eoit.activity.util; import android.database.Cursor; import android.support.v4.widget.SimpleCursorAdapter; import android.view.View; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; import fr.eoit.EOITConst; import fr.eoit.R; import fr.eoit.activity.listener.FavoriteStationsOnCheckedChangeListener; import fr.eoit.db.bean.Station; import fr.eoit.util.IconUtil; import java.text.DecimalFormat; import java.text.NumberFormat; /** * @author picon.software * */ public class StationListViewBinder implements SimpleCursorAdapter.ViewBinder { private String stationName; private NumberFormat nf = new DecimalFormat("+0.00"); private NumberFormat nfVolume = new DecimalFormat(EOITConst.VALUES_PATTERN); @Override public boolean setViewValue(View view, Cursor cursor, int columnIndex) { int viewId = view.getId(); boolean isNull = cursor.isNull(columnIndex); TextView textView; ImageView imageView; String[] stationNameArray; double price; long volume; int role, stationId = cursor.getInt(cursor.getColumnIndexOrThrow(Station._ID)); switch (viewId) { case R.id.station_name: stationName = cursor.getString(columnIndex); textView = (TextView) view; stationNameArray = stationName.split(" - "); textView.setText(stationNameArray[stationNameArray.length - 1]); break; case R.id.location_name: String regionName = cursor.getString(columnIndex); textView = (TextView) view; stationNameArray = stationName.split(" - "); StringBuilder sb = new StringBuilder(regionName); if (stationNameArray.length == 2) { sb.append(" > ").append(stationNameArray[0]); } else if (stationNameArray.length == 3) { sb.append(" > ").append(stationNameArray[0]).append(" - ").append(stationNameArray[1]); } textView.setText(sb.toString()); break; case R.id.station_icon: int id = cursor.getInt(columnIndex); imageView = (ImageView) view; IconUtil.initRender(id, imageView); break; case R.id.favorite_station: boolean favorite = cursor.getInt(columnIndex) == 1; CheckBox favoriteCheckBox = (CheckBox) view; favoriteCheckBox.setChecked(favorite); favoriteCheckBox.setOnCheckedChangeListener( new FavoriteStationsOnCheckedChangeListener(stationId, favoriteCheckBox.getContext())); break; case R.id.station_prod_icon: role = cursor.getInt(columnIndex); imageView = (ImageView) view; if (role == EOITConst.Stations.TRADE_ROLE || isNull) { view.setVisibility(View.GONE); } else { view.setVisibility(View.VISIBLE); } break; case R.id.station_trade_icon: role = cursor.getInt(columnIndex); imageView = (ImageView) view; if (role == EOITConst.Stations.PRODUCTION_ROLE || isNull) { view.setVisibility(View.GONE); } else { view.setVisibility(View.VISIBLE); } break; case R.id.corp_standing: float standing = cursor.getFloat(columnIndex); textView = (TextView) view; textView.setText(nf.format(standing)); if (standing > 0) { textView.setTextColor(view.getResources().getColor(R.color.green)); } else if (standing == 0) { textView.setTextColor(view.getResources().getColor(R.color.grey)); } else if (standing < 0) { textView.setTextColor(view.getResources().getColor(R.color.red)); } break; case R.id.buy_price: case R.id.sell_price: price = cursor.getDouble(columnIndex); textView = (TextView) view; PricesUtils.setPrice(textView, price, true); break; case R.id.buy_volume: case R.id.sell_volume: volume = cursor.getLong(columnIndex); textView = (TextView) view; textView.setText(nfVolume.format(volume)); break; default: throw new IllegalArgumentException("viewId : " + viewId); } return true; } }