@Override public View getView(int position, View convertView, ViewGroup parent) { Device device = getItem(position); if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.item_device_list, parent, false); } TextView tvName = (TextView) convertView.findViewById(R.id.device_name); TextView tvIP = (TextView) convertView.findViewById(R.id.device_ip); TextView tvTraffic = (TextView) convertView.findViewById(R.id.device_traffic); View imgBlocked = convertView.findViewById(R.id.blocked); ProgressBar pbTrafficBar = (ProgressBar) convertView.findViewById(R.id.traffic_bar); tvName.setText(device.name()); tvIP.setText(device.lastIP()); imgBlocked.setVisibility(device.isBlocked() ? View.VISIBLE : View.GONE); if (device.isActive()) { pbTrafficBar.setVisibility(View.VISIBLE); tvName.setTextColor(Color.BLACK); tvTraffic.setText(String.format("%.2f", device.lastSpeed() / 1000) + " kb/s"); if (mTotalTraffic > 0) pbTrafficBar.setProgress(Math.round(device.lastSpeed() / mTotalTraffic * 100)); else pbTrafficBar.setProgress(0); if (device.prioritizedUntil() == Device.NOT_PRIORITIZED) { convertView.findViewById(R.id.priority).setVisibility(View.INVISIBLE); } else { if (device.prioritizedUntil() == Device.INDETERMINATE_PRIORITY) { ((TextView) convertView.findViewById(R.id.priority_until)) .setText(R.string.indeterminite_priority_access); convertView.findViewById(R.id.until).setVisibility(View.INVISIBLE); } else { convertView.findViewById(R.id.until).setVisibility(View.VISIBLE); long now = System.currentTimeMillis() / 1000; if (device.prioritizedUntil() < now) { convertView.findViewById(R.id.priority).setVisibility(View.INVISIBLE); ((TextView) convertView.findViewById(R.id.priority_until)) .setText(R.string.indeterminite_priority_access); } else { convertView.findViewById(R.id.priority).setVisibility(View.VISIBLE); Calendar undoTime = Calendar.getInstance(); undoTime.setTimeInMillis(device.prioritizedUntil()); String time = undoTime.get(Calendar.HOUR) + ":" + String.format("%02d", undoTime.get(Calendar.MINUTE)); ((TextView) convertView.findViewById(R.id.priority_until)) .setText(R.string.priority_access_until); ((TextView) convertView.findViewById(R.id.until)).setText(time); } } } } else { tvName.setTextColor(Color.GRAY); pbTrafficBar.setVisibility(View.GONE); tvTraffic.setText(""); convertView.findViewById(R.id.priority).setVisibility(View.INVISIBLE); } return convertView; }
public DeviceListAdapter(Context context, List<Device> devices) { super(context, 0, devices); mContext = context; mTotalTraffic = 0; for (Device d : devices) if (d.isActive()) mTotalTraffic += d.lastSpeed(); }