从ViewPager Android中的String.xml获取String

时间:2018-03-28 21:13:07

标签: android android-viewpager string.xml

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;
}

}

我想让我的应用支持多种语言。 我想访问string.xml而不是Arrays slide_headings2和slide_desc2中的硬编码字符串。
我到处寻找,我尝试了context.getResources()。getString(R.string.string_name)。

如何用string.xml中的R.string替换数组中的硬编码字符串?

1 个答案:

答案 0 :(得分:0)

let wifiNotifcation = UIAlertController(title: "Please Connect to Wi-Fi", message: "Please connect to your standard Wi-Fi Network", preferredStyle: .alert)
wifiNotifcation.addAction(UIAlertAction(title: "Open Wi-Fi", style: .default, handler: { (nil) in
     let url = URL(string: "App-Prefs:root=WIFI")

     if UIApplication.shared.canOpenURL(url!){
           UIApplication.shared.openURL(url!)
           self.navigationController?.popViewController(animated: false)
     }
}))
self.present(wifiNotifcation, animated: true, completion: nil)

像这样简单^