0
public class DietSlideAdapter extends PagerAdapter {

Context context;
LayoutInflater layoutInflater;


public DietSlideAdapter(Context context){
    this.context = context;

}


//Arrays
public int[] slide_images2 = {
        R.drawable.breakfast,
        R.drawable.cabbage,
        R.drawable.protein,
        R.drawable.fish,
        R.drawable.tea

};

public String [] slide_headings2 = {
        "heading1","heading2","heading3","heading4","heading5"

};

public String [] slide_descs2 = {
        "Try fortified ready-to-eat or cooked breakfast cereals with fruit. Fortified cereals have added nutrients, like calcium.",
        "Choose a variety of vegetables and fruits, like carrots, cooked greens, bananas, and melon.\n" +
                "Eat plenty of beans and whole grains. Try brown rice or oatmeal.",
        "Iron keeps your blood healthy. Folic acid helps prevent birth defects.\n" +
                "Even before you find out you're pregnant, it's smart to start eating plenty of folate-rich foods like fortified cereals, asparagus, lentils, wheat germ, oranges, and orange juice.",
        "Avoid fish and shellfish with high levels of mercury. Common fish that are low in mercury include shrimp, and catfish. ",
        "Drink decaffeinated coffee or tea.\n" +
                "Drink water or seltzer instead of soda.\n" +
                "Don't drink alcohol."


};




@Override
public Object instantiateItem(ViewGroup container, int position){
    layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.slide2, container, false);

    ImageView slideImageView2 = (ImageView) view.findViewById(R.id.slideImage2);
    TextView slideHeading2 = (TextView) view.findViewById(R.id.slide_heading2);
    TextView slideDescription2 = (TextView) view.findViewById(R.id.slide_desc2);

    slideImageView2.setImageResource(slide_images2[position]);
    slideHeading2.setText(slide_headings2[position]);
    slideDescription2.setText(slide_descs2[position]);
    slideDescription2.setMovementMethod(new ScrollingMovementMethod());


    container.addView(view);

    return  view;
}

}

I want to make my app support multiple language. I want to access string.xml instead of the Hard-coded strings in the Arrays slide_headings2 and slide_desc2.
I looked everywhere and I tried context.getResources().getString(R.string.string_name).

How do i replace the hard coded strings in the array with a R.string from string.xml?

1 Answers1

0
    String[] slide_images2 = {
            context.getString(R.string.heading1),
            context.getString(R.string.heading2),
            context.getString(R.string.heading3),
            context.getString(R.string.heading4),
            context.getString(R.string.heading5),
    };

Simple like this ^

MoGa
  • 635
  • 6
  • 14
  • I followed yours, but doesnt work. on getstring() when i put cursor, it says javaNullException may occur. also when i run the app it crashes. – H M Sadman Haque Mar 28 '18 at 21:49
  • You are probably initializing these outside of `onCreate` or `onResume`. Make sure you do it inside onCreate, if you need global access to the array, then create it outside, and initialize it inside onCreate. This way you can make sure the context is available – MoGa Mar 28 '18 at 21:59
  • Yes it works now. After your suggestion I implemented the arrays in the main class rather than the adapterhelper class. thank you – H M Sadman Haque Mar 29 '18 at 04:33