Search code examples
javaandroidasynchronousstaticandroid-arrayadapter

Android Cannot update ArrayAdapter because "Non-Static method 'notifyDataSetChanged()' cannot be referenced from a static context"


I'm using a bit of code to help me with pulling data from the web called WebRequest (https://github.com/delight-im/Android-WebRequest). It provides a callback for retrieving asynchronous data. However I can't update my ArrayAdapter because I get an error "Non-Static method 'notifyDataSetChanged()' cannot be referenced from a static context"

Now, I've seen a number of similar questions here. Some suggest putting the notifyDataSetChanged command in the Adapter class itself. Some suggest using a Handler and some suggest using a Loader. However I have had no luck actually implementing these solutions. Here's my code:

    new WebRequest().get().to(stringSQLUrl).executeAsync(new WebRequest.Callback() {
        public void onSuccess(int responseCode, String responseText) {
            try {
                DataHistory = CsvUtil.fromCsvTable(responseText);
                DataHistory.remove(0);  //Removes header row
            } catch (Exception e) {
                Log.e("Main pullWebData","Error converting from CsvTable: " + e.getMessage());
            }
            DataAdapter.notifyDataSetChanged();  //  <-- ERROR HERE
            runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        DataAdapter.notifyDataSetChanged();   // <-- ALSO ERROR HERE
                    }
                });
        }
        public void onError() {
            Log.e("Main pullWebData","Error pulling data from web");
        }
    });

I also defined this Handler in my activity thinking I could call it and it would update the ArrayAdapter, but I got the same error here:

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        WeightAdapter.notifyDataSetChanged();
    }
};

Lastly I created a method inside the Adapter definition to notify itself, but calling that gave me the same static/non-staic error:

public void updateMe() {
    this.notifyDataSetChanged();
}

Long story short - there are a ton of questions seemingly about this same topic and lots of advice, but I have been unsuccessful in implementation. Can anyone show me exactly how I'd implement this?

Thank you!

One other thing: I was considering switching from Web data to an Azure SQL DB, but that would also use a callback and I presume have the same issue?


Solution

  • You can only call static methods using ClassName.methodName(); However, notifyDataSetChanged() is not a static method. i.e. notifyDataSetChanged() works depending on the instance of your adapter.

    To make sure that this works, you should use notifyDataSetChanged() on the object of the custom adapter.

    If you have something like :

    DataAdapter customAdapter = new DataAdapter(//params);
    listView.setAdapter(customAdapter);
    

    You should call :

    customAdapter.notifyDataSetChanged();