1

I need to pass a string that i have created in strings.xml to java class that has a string array method. I have created the string but don't know how to pass it into the array

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"

};
2
  • You cannot access getResources before the Activity has initialised. You'll want to pull in the bank string in/after your onCreate method has run Commented Mar 11, 2016 at 14:45
  • bank is not a string array resource, it is a string resource. Commented Mar 11, 2016 at 15:11

3 Answers 3

2
public class FacilityAdapter extends BaseAdapter {

    private String[] mThumbNames;

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

    //...

}
Sign up to request clarification or add additional context in comments.

Comments

1

the complete code is like this

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

}

Comments

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

also, as biddulph.r says, do this after onCreate method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.