【问题标题】:Passing dynamic string resource to "setText()"将动态字符串资源传递给“setText()”
【发布时间】:2018-09-19 10:15:43
【问题描述】:

我正在尝试制作一个动态字符串并转换已传递给 setText() 函数的硬编码字符串。但是它似乎不接受它。 这是我尝试过的:

原始代码:

 device.name.setText("Mac :" + data.macAddress); 

Strings.xml 声明:

<string name="mac">Mac :</string>

新代码:

device.name.setText(R.string.mac + data.macAddress); 

代码 sn-p:

   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,我无法发布屏幕截图。

【问题讨论】:

  • HTML in string resource? 的可能重复项
  • 首先您应该正确提供代码,然后提出问题。
  • @MD :您不应该对新用户投反对票并劝阻他们。分数下降对寻求帮助的人施加了更多限制。如果你能给我一个建议而不是投反对票,我会很感激的
  • @Dexter Ya 很好。但是您应该至少看一次How to ask?,因为每天都有&gt;1000 新的biee 进来,如果他们问&gt;100000 毫无意义的问题。所以最好Check this
  • @MD:即使没有该代码 sn-p,我的问题也确实很有意义……我按照 SO 发布了所有详细信息。有什么问题,我做了什么尝试,我在哪里得到错误。 SO 要求用户具体解决问题。

标签: android


【解决方案1】:

试试这样:

创建一个全局变量,如:

private String name;

然后在“onCreateViewHolder”中这样写:

name= parent.getResources().getString(R.string.mac);

现在,在“onBindViewHolder”中这样写:

device.name.setText(name + data.macAddress);

【讨论】:

  • 再次感谢。不幸的是 device.getResources() 不可用:(
  • 您好,同样的问题,parent.getResources() 不可用
  • 您是在“onCreateViewHolder”中在返回语句方法之前编写该代码吗?
  • 是的,我写在下面的顶部:private final TextView mEmpty;
  • 太棒了......终于......非常感谢:)
【解决方案2】:

请用这个

device.name.setText(data.macAddress + ""); 

或

device.status.setText(R.string.poff + "");

【讨论】:

  • 我想让字符串 "Mac:" 动态更改为我在 strings.xml 中输入的任何值。但是就像我说的那样,它给了我一个整数值
猜你喜欢
  • 2020-02-02
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
  • 2017-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多