4

I have a problem with caling the getResources() function in an standard class. All imports must be there to use the function. Is there any special class I need to extend my class?

Thanks for the immediate help.

package com.example.helloandroid;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.ContextWrapper;

import android.content.res.Resources;

import android.content.Intent;
import android.os.Bundle;

//import android.content.res.Resources;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DbAdapter {

    public DbAdapter() {
     Resources res = getResources();//error: The method getResources() is undefined for the type DbAdapter
            //also tyed context.getResources()
    }


}
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Udo
  • 73
  • 1
  • 1
  • 7

3 Answers3

31

getResouces is a method of a Context. So you can pass the context to your DbAdapter constructor and call getResources from it :

public DbAdapter(Context context) {
     Resources res = context.getResources();//error: The method getResources() is undefined for the type DbAdapter
            //also tied context.getResources()
}
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
ccheneson
  • 49,072
  • 8
  • 63
  • 68
  • use getApplicationContext to get the application context that you will be sending to DbAdapter constructor. – Ben Apr 23 '13 at 11:34
1

If your Adapter is for example an ArrayAdapter, you can use this:

getContext().getResources();

Patapoom
  • 794
  • 1
  • 7
  • 16
0

Define Context's object and then call all the R.<Methods> with the help of context object .

Eg:

Context ctx;
ctx.getResources().getString(R.string.Forgot_message);

Above code is working for me .

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62