Back to project page android_device.
The source code is released under:
[Apache License](http://www.apache.org/licenses/): Version 2.0, January 2004 =============== ## TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION ## ### 1. Definitions. ### "License" sha...
If you think the Android project android_device listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * ================================================================================================= * Copyright (C) 2013 - 2014 Martin Albedinsky [Wolf-ITechnologies] * ================================================================================================= * Licensed under the Apache License, Version 2.0 or later (further "License" only). * ------------------------------------------------------------------------------------------------- * You may use this file only in compliance with the License. More details and copy of this License * you may obtain at//from w ww . ja va 2 s.c o m * * http://www.apache.org/licenses/LICENSE-2.0 * * You can redistribute, modify or publish any part of the code written within this file but as it * is described in the License, the software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND. * * See the License for the specific language governing permissions and limitations under the License. * ================================================================================================= */ package com.wit.android.device.examples.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.wit.android.device.examples.R; import com.wit.android.device.examples.model.BatteryInfo; import com.wit.android.device.Battery; import com.wit.android.examples.libs.adapter.ExSimpleAdapter; import java.util.ArrayList; /** * @author Martin Albedinsky */ public class BatteryInfoAdapter extends ExSimpleAdapter<BatteryInfo> { /** * Log TAG. */ // private static final String TAG = BatteryInfoAdapter.class.getSimpleName(); /** * */ private static final int VIEW_TYPE_COUNT = 2; /** * */ private static final int VIEW_TYPE_INFO = 0x00; /** * */ private static final int VIEW_TYPE_CHANGE = 0x01; /** */ public BatteryInfoAdapter(Context context) { super(context, new ArrayList<BatteryInfo>(0)); } /** * @param info */ public void dispatchNewInfo(BatteryInfo info) { // Add latest info always at the top of the list. getItems().add(0, info); notifyDataSetChanged(); } /** */ @Override public int getViewTypeCount() { return VIEW_TYPE_COUNT; } /** */ @Override public int getItemViewType(int position) { final BatteryInfo info = getItem(position); return info.hasInfo() ? VIEW_TYPE_INFO : VIEW_TYPE_CHANGE; } /** */ @Override public View onCreateView(int position, LayoutInflater inflater, ViewGroup parent) { switch (currentViewType()) { case VIEW_TYPE_INFO: return inflate(R.layout.item_list_battery_info, parent); case VIEW_TYPE_CHANGE: return inflate(R.layout.item_list_battery_change, parent); } return null; } /** */ @Override public void onBindView(int position, Object viewHolder) { final BatteryInfo item = getItem(position); if (item != null) { switch (currentViewType()) { case VIEW_TYPE_INFO: final InfoHolder infoHolder = (InfoHolder) viewHolder; infoHolder.setTime(item.getTime()); infoHolder.setHealth(item.getHealth()); infoHolder.setStrength(item.getStrength()); infoHolder.setPluggedState(item.getPluggedState()); infoHolder.setStatus(item.getStatus()); break; case VIEW_TYPE_CHANGE: final ChangeHolder changeHolder = (ChangeHolder) viewHolder; changeHolder.setTime(item.getTime()); changeHolder.setChange(item.getChange()); break; } } } /** */ @Override public Object onCreateViewHolder(int position, View itemView) { switch (currentViewType()) { case VIEW_TYPE_INFO: return new InfoHolder(itemView); case VIEW_TYPE_CHANGE: return new ChangeHolder(itemView); } return null; } /** * */ static class ChangeHolder { TextView mTime, mChange; ChangeHolder(View itemView) { this.mTime = (TextView) itemView.findViewById(R.id.item_list_battery_change_text_view_time); this.mChange = (TextView) itemView.findViewById(R.id.item_list_battery_change_text_view_change); } /** * @param timeText Time value text. */ void setTime(String timeText) { mTime.setText(timeText); } /** * @param change */ void setChange(String change) { mChange.setText(change); } } /** * Battery info adapter's item view holder. */ static class InfoHolder { TextView mTime, mHealth, mStrength, mPluggedState, mStatus; /** * Creates new item view holder. * * @param itemView Adapter item view. */ InfoHolder(View itemView) { this.mTime = (TextView) itemView.findViewById(R.id.item_list_battery_info_text_view_time); this.mHealth = (TextView) itemView.findViewById(R.id.item_list_battery_info_text_view_health); this.mStrength = (TextView) itemView.findViewById(R.id.item_list_battery_info_text_view_strength); this.mStatus = (TextView) itemView.findViewById(R.id.item_list_battery_info_text_view_status); this.mPluggedState = (TextView) itemView.findViewById(R.id.item_list_battery_info_text_view_plugged_state); } /** * @param timeText Time value text. */ void setTime(String timeText) { mTime.setText(timeText); } /** * @param health */ void setHealth(Battery.BatteryHealth health) { mHealth.setText(health != null ? health.name() : "???"); } /** * @param strength */ void setStrength(int strength) { mStrength.setText(strength >= 0 ? Integer.toString(strength) : "???"); } /** * @param state */ void setPluggedState(Battery.BatteryPluggedState state) { mPluggedState.setText(state != null ? state.name() : "???"); } /** * @param status */ void setStatus(Battery.BatteryStatus status) { mStatus.setText(status != null ? status.name() : "???"); } } }