0

I have a class like this :

import java.util.ArrayList;

/*  Get ArrayList  of UserModel type  */
public class GetUserModelData {
    public static ArrayList<UserModel> getUserModelData() {
        ArrayList<UserModel> arrayList = new ArrayList<>();
        arrayList.add(new UserModel("Droid", "droid@gmail.com"));
        arrayList.add(new UserModel("John", "john@gmail.com"));
        arrayList.add(new UserModel("David" , "david@gmail.com"));
        arrayList.add(new UserModel("Humpy", "humpy@gmail.com"));
        arrayList.add(new UserModel("Sharma", "sharma@gmail.com"));
        arrayList.add(new UserModel("Dravid", "dravid@gmail.com"));
        return arrayList;
    }
}

I want use getString instead hardcode text. Because this ArrayList dataset is use for multiple Fragment.

Can anyone helpme.

  • 1
    What are you exactly trying to achieve? – DCruz22 Nov 04 '20 at 00:16
  • i want to use getString under UserModel – RezaAlhadar Nov 04 '20 at 00:43
  • @MuhammadAlRidha It is very unclear of what you're trying to achieve. getString would imply that you want to retrieve a hardcoded string, yet your question suggest you want you use that instead of hardcoded text. – Garren Fitzenreiter Nov 04 '20 at 00:46
  • Does this answer your question? [Android: getString(R.string) in static method](https://stackoverflow.com/questions/3822732/android-getstringr-string-in-static-method) – Ryan M Nov 04 '20 at 00:50
  • Hii Ryan, i already try that. It can't work either. – RezaAlhadar Nov 04 '20 at 01:06
  • @GarrenFitzenreiter `arrayList.add(new UserModel(getString(R.string.driod), getString(R.string.driod_email)));` i want to achieve like this. Inside this class. – RezaAlhadar Nov 04 '20 at 01:11

1 Answers1

0

If you want to use getString instead of hard-coded text in a separate class, you need to pass Context as a parameter like this:

public static ArrayList<UserModel> getUserModelData(Context context) {
}

then use it like that:

arrayList.add(new UserModel(context.getResources().getString(R.string.droidName), context.getResources().getString(R.string.droidEmail)));

Do not forget to put your texts in strings.xml file

 <string name="droidName">Droid</string>
 <string name="droidEmail">droid@gmail.com</string>
Marwa Eltayeb
  • 1,921
  • 1
  • 17
  • 29