-2

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 .

Dexter
  • 45
  • 1
  • 9
  • possible duplicate of [HTML in string resource?](http://stackoverflow.com/questions/2667319/html-in-string-resource) – Maveňツ Jun 22 '15 at 10:29
  • First you should provide code properly then asked question. – M D Jun 22 '15 at 10:34
  • @MD : You shouldn't down-vote new users and discourage them . Points going down put more restrictions on people to find help . I would have simply appreciated if you would have left me an advice instead of down-voting – Dexter Jun 22 '15 at 10:37
  • @Dexter Ya that is fine. but you should take a look [How to ask?](http://stackoverflow.com/help/how-to-ask) at least once becoz every day there are `>1000` new biee came in SO and if they asked `>100000` questions with No sense. So it's better to [Check this](http://stackoverflow.com/help/how-to-ask) – M D Jun 22 '15 at 10:39
  • @MD : My question did make perfect sense even without that code snippet ... I posted all details as per SO . What is problem , what I did try , Where I am getting the error. SO asks users to be specific to the problem . – Dexter Jun 22 '15 at 10:57
  • @Dexter You think about that. `-3 down votes` proof that your question missed some useful information. – M D Jun 22 '15 at 10:59

2 Answers2

1

Try like this:

Create a global variable like:

private String name;

then in "onCreateViewHolder" write like this:

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

now, in "onBindViewHolder" write like this:

device.name.setText(name + data.macAddress);
Anand Singh
  • 5,672
  • 2
  • 23
  • 33
  • Thanks again . Unfortunately device.getResources() is not available :( – Dexter Jun 22 '15 at 11:06
  • Hi , same problem , parent.getResources() is not available – Dexter Jun 22 '15 at 11:47
  • you are writing that code inside "onCreateViewHolder" before return statement method? – Anand Singh Jun 22 '15 at 11:48
  • Yes I am writing it at the top below : private final TextView mEmpty; – Dexter Jun 22 '15 at 11:49
  • @Override public Device onCreateViewHolder(ViewGroup parent, int position) { name = parent.getResources().getString(R.string.mac); return new Device(mInflater.inflate(R.layout.item_device, parent, false), mListener); } like this? – Anand Singh Jun 22 '15 at 11:52
  • 1
    Awesome ... Finally ... Thanks a ton :) – Dexter Jun 22 '15 at 11:59
  • @Anand Singh I may have a similar problem here: http://stackoverflow.com/questions/43531900/android-how-to-fix-recyclerview-onbindviewholder. I would appreciate any thoughts or ideas on how to fix. – AJW Apr 24 '17 at 04:39
0

Please use this

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

or

device.status.setText(R.string.poff + "");
Pang
  • 9,564
  • 146
  • 81
  • 122
Mayank Sugandhi
  • 397
  • 1
  • 7
  • 22
  • I want to have the string "Mac : " dynamically changed to whatever value I put in strings.xml . However like I said it is instead giving me an integer value – Dexter Jun 22 '15 at 11:00