Android: getApplicationContext ()

2

I'm trying to do

Database db = new Database(this.getApplicationContext());

Where Database is a class I have with all the Database-related methods and what I'm calling to populate an ExpandableList. However, I'm getting error in this , the error is " Cannot referenced from a static context ". How can I get the context so I do not get this error?

public class ExpandableList {

public static HashMap<String, List<String>> getData() {
    HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();


    List<String> exemplos = new ArrayList<String>();
    Database db = new Database(this.getApplicationContext());
(...)
    
asked by anonymous 23.10.2015 / 22:49

1 answer

2

can pass via parameter:

getData(ApplicationContext appContext)

getApplicationContext () is an Activity method, so it only exists in Activity Extending Classes (AppCompatActivity, ActionBarActivity, etc.)

try the following:

    public static HashMap<String, List<String>> getData(ApplicationContext appContext) {
    // conteudo do seu metodo 
Database db = new Database(appContext);
    }

Already in your Activity:

expandableList.getData(this.getApplicationContext());

I hope I have helped!

Greetings!

    
23.10.2015 / 22:57