Search code examples
androidadapternotifydatasetchanged

How to access to an adapter in an Activity when the adapter is created in a class which is called from the Activity?


In an Android application, there are the following items:

Activity_A (Activity) ,

Java_C_file_A (Class which is used in Activity_A) ,

GridView_A (dynamically created Gridview inside Java_C_file_A) ,

CustomAdapter_A (CustomAdapter for GridView_A)

Now when an item in GridView is clicked, I utilize interface to identify the item in Activity_A
Then I change the data for CustomAdapter_A and I need to use notifyDataSetChanged() in Activity_A

But since CustomAdapter_A is created in Java_C_file_A (which is outside of Activity_A), then I don't have access to CustomAdapter_A inside Activity_A
And I can't use notifyDataSetChanged() inside Activity_A as a result

How can I have access to CustomAdapter_A inside Activity_A ?


Solution

  • You can create getter for CustomAdapter_A inside Java_C_file_A class and you will have object of Java_C_file_A in your Activity_A. Here is sample code to access adapter

    In Activity_A

    Java_C_file_A javaCFileA = new Java_C_file_A();
    CustomAdapter_A customAdapter = javaCFileA.getCustomAdapter();