package nodomain.freeyourgadget.gadgetbridge.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.List; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; import nodomain.freeyourgadget.gadgetbridge.util.GB; /** * Adapter for displaying GBDeviceCandate instances. */ public class DeviceCandidateAdapter extends ArrayAdapter { private final Context context; public DeviceCandidateAdapter(Context context, List deviceCandidates) { super(context, 0, deviceCandidates); this.context = context; } @Override public View getView(int position, View view, ViewGroup parent) { GBDeviceCandidate device = getItem(position); if (view == null) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.item_with_details, parent, false); } ImageView deviceImageView = (ImageView) view.findViewById(R.id.item_image); TextView deviceNameLabel = (TextView) view.findViewById(R.id.item_name); TextView deviceAddressLabel = (TextView) view.findViewById(R.id.item_details); String name = formatDeviceCandidate(device); deviceNameLabel.setText(name); deviceAddressLabel.setText(device.getMacAddress()); deviceImageView.setImageResource(device.getDeviceType().getIcon()); return view; } private String formatDeviceCandidate(GBDeviceCandidate device) { if (device.getRssi() > GBDevice.RSSI_UNKNOWN) { return context.getString(R.string.device_with_rssi, device.getName(), GB.formatRssi(device.getRssi())); } return device.getName(); } }