I am trying to make a dynamic string and converting a hardcoded string which has been passed to setText() function . However it doesn't seem to accept it . Here is what I have tried :
Original code :
device.name.setText("Mac :" + data.macAddress);
Strings.xml declaration :
<string name="mac">Mac :</string>
New code :
device.name.setText(R.string.mac + data.macAddress);
Code snippet :
public class DeviceAdapter extends RecyclerView.Adapter<Device> {
private final LayoutInflater mInflater;
private final Device.IDeviceItem mListener;
private final TextView mEmpty;
public Vector<DeviceData> mDeviceData;
public DeviceAdapter(Context context, TextView emptyView, Device.IDeviceItem listener) {
mEmpty = emptyView;
mListener = listener;
mInflater = LayoutInflater.from(context);
}
@Override
public Device onCreateViewHolder(ViewGroup parent, int position) {
return new Device(mInflater.inflate(R.layout.item_device, parent, false), mListener);
}
@Override
public void onBindViewHolder(Device device, int position) {
DeviceData data = mDeviceData.elementAt(position);
device.position = position;
device.deviceId = data.deviceId;
device.name.setText("Mac :" + data.macAddress); // check this
if (data.temperature == null) {
device.temperature.setText(R.string.temp_notset);
}else device.temperature.setText("Temperature set to " + data.temperature + "\u00B0" +" celsius.");
if (data.powerState) {
device.btnPower.setImageResource(R.drawable.power_off);
if (!data.startState) {
device.status.setText(R.string.pon_running);
device.btnStart.setImageResource(R.drawable.start_off);
} else {
device.status.setText(R.string.pon_stop);
device.btnStart.setImageResource(R.drawable.start_on);
}
} else {
device.btnPower.setImageResource(R.drawable.power_on);
device.status.setText(R.string.poff);
}
}
@Override
public int getItemCount() {
int sz = mDeviceData.size();
if (sz == 0) {
mEmpty.setVisibility(View.VISIBLE);
} else {
mEmpty.setVisibility(View.GONE);
}
return sz;
}
public boolean hasDevice(DeviceData deviceData) {
for (DeviceData device : mDeviceData) {
if (device.deviceId.equalsIgnoreCase(deviceData.deviceId) && device.boxid.equalsIgnoreCase(deviceData.boxid))
return true;
}
return false;
}
}
So when I have hard-coded string : It displays the right value , However when changed dynamically it gets converted to an integer number "213136187044" . What am I doing wrong ? I am unable to post screenshots since my reputation is below 10 .