如何从pagerAdapter内部调用getString()

时间:2019-02-23 09:53:46

标签: java android

package com.example.android.booklisting;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SliderAdapter extends PagerAdapter {

    Context context;
    LayoutInflater layoutInflater;

    public SliderAdapter(Context context) {
        this.context = context;
    }

    public String[] slide_headings = {
            context.getString(R.string.onboard1Header),
            context.getString(R.string.onboard2Header),
            context.getString(R.string.onboard3Header)
    };
}

我尝试传递上下文,还尝试了getResources()。getString(),但仍然无法正常工作。我在做什么错了?

2 个答案:

答案 0 :(得分:0)

尝试使用getResources()方法获取字符串:

String yourString = context.getResources().getString(R.string.yourResource);

答案 1 :(得分:0)

更改

public String[] slide_headings = {
        context.getString(R.string.onboard1Header),
        context.getString(R.string.onboard2Header),
        context.getString(R.string.onboard3Header)
};

收件人

private int[] slide_headings = {
        R.string.onboard1Header,
        R.string.onboard2Header,
        R.string.onboard3Header
};

并像这样使用它:

String yourString = context.getResources().getString(slide_headings[yout_index]);