我想製作一個動態字符串並轉換已傳遞給setText()函數的硬編碼字符串。但它似乎並不接受它。 這是我曾嘗試:將動態字符串資源傳遞給「setText()」
原始代碼:
device.name.setText("Mac :" + data.macAddress);
的strings.xml聲明:
<string name="mac">Mac :</string>
新代碼:
device.name.setText(R.string.mac + data.macAddress);
代碼片段:
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;
}
}
因此,當我有硬編碼的字符串:它顯示正確的值,但是當動態更改時,它會轉換爲整數「213136187044」。我究竟做錯了什麼 ?由於我的聲望低於10,我無法發佈屏幕截圖。
po可以複製的[字符串資源中的HTML?](http://stackoverflow.com/questions/2667319/html-in-string-resource) – Gattsu
首先,您應該提供正確的代碼,然後問問題。 –
@MD:你不應該對新用戶進行投票並拒絕他們。積分下降會給人們更多的限制以尋求幫助。如果你願意給我一個建議而不是降級投票 – Dexter