1

I have this POJO Model Class in model folder.

package com.example.test.model;

public class ModelCampaign {

    private String mNameCampaign;
    private int mIdCampaign;
    private String mTypeCampaign;
    private String mFormattedTypeCampaign;

    public ModelCampaign() {
    }

    public ModelCampaign(String nameCampaign, int idCampaign, String typeCampaign) {
        this.mNameCampaign = mNameCampaign;
        this.mIdCampaign = mIdCampaign;
        this.mTypeCampaign = mTypeCampaign;
    }

    // some stuff

    private String setFormattedTypeCampaign(){

        switch (this.mTypeCampaign){
            case "london" : this.mFormattedTypeCampaign = getString(R.string.london);
                break;
        }

    }

}

I need to access to the strings.xml file. Of course I have no context. How I can access to them?

Thank you very much!

sineverba
  • 5,059
  • 7
  • 39
  • 84

1 Answers1

1

If you want to access Resources from your Model class, then pass Resources object into the Constructor. Your constructor should look like this

public ModelCampaign(Resources res,String nameCampaign, int idCampaign, String typeCampaign){
        this.res = res;
        this.mNameCampaign = mNameCampaign;
        this.mIdCampaign = mIdCampaign;
        this.mTypeCampaign = mTypeCampaign;
    }

and you could get String like this by passing res id

public String getString(int key){
 return res.getString(key);
}

and initialize the Object like this

ModelCampaign mc = new ModelCampaign(getResources(),......);
theapache64
  • 10,926
  • 9
  • 65
  • 108