Android:将字符串从xml传递到字符串数组

时间:2016-03-11 14:43:04

标签: android string android-xml

我需要将我在strings.xml中创建的字符串传递给具有字符串数组方法的java类。我创建了字符串但不知道如何将其传递给数组

的strings.xml

<string name = "bank">
    <b>Bank</b>
</string>

Facilityadapter.java

public String[] mThumbNames = {
       getResources().getStringArray(R.string.bank),
        "Hostel",
        "GYM",
        "Library",
        "Sports",
        "Guest House"

};

3 个答案:

答案 0 :(得分:2)

public class FacilityAdapter extends BaseAdapter {

    private String[] mThumbNames;

    public FacilityAdapter(Context c){
        mThumbNames = new String[] {c.getString(R.string.bank), "Hostel", "GYM"};
    }

    //...

}

答案 1 :(得分:1)

完整的代码就像这样

    public class FacilityAdapter extends BaseAdapter {
    private Context mContext;


    // Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.bank, R.drawable.girlshostel,
            R.drawable.gym , R.drawable.library,
            R.drawable.sports , R.drawable.guesthouse

    };

    public String[] mThumbNames = new String[]{
            getResources().getStringArray(R.string.bank)[0],
            "GirlsHostel",
            "GYM",
            "<u>Library</u>",
            "Sports",
            "Guest House"

    };



    // Constructor
    public FacilityAdapter(Context c){
        mContext = c;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View gridView;
        gridView = new View(mContext);



        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
        return imageView;
    }

}

答案 2 :(得分:0)

getResources().getString(R.string.bank)

另外,正如biddulph.r所说,在onCreate方法

之后执行此操作