简体   繁体   English

在从 Activity 调用的类中创建适配器时,如何访问 Activity 中的适配器?

[英]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:在Android应用程序中,有以下项目:

Activity_A (Activity) , Activity_A (活动),

Java_C_file_A (Class which is used in Activity_A) , Java_C_file_A (Activity_A 中使用的类),

GridView_A (dynamically created Gridview inside Java_C_file_A) , GridView_A (在 Java_C_file_A 中动态创建的 Gridview),

CustomAdapter_A (CustomAdapter for GridView_A) CustomAdapter_A (GridView_A 的自定义适配器)

Now when an item in GridView is clicked, I utilize interface to identify the item in Activity_A现在当GridView一个项目被点击时,我利用界面来识别Activity_A的项目
Then I change the data for CustomAdapter_A and I need to use notifyDataSetChanged() in Activity_A然后我更改CustomAdapter_A的数据,我需要在Activity_A使用notifyDataSetChanged()

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但是由于CustomAdapter_A是在Java_C_file_A (在Activity_A之外)创建的,所以我无权访问Activity_A CustomAdapter_A
And I can't use notifyDataSetChanged() inside Activity_A as a result结果我不能在Activity_A使用notifyDataSetChanged()

How can I have access to CustomAdapter_A inside Activity_A ?我如何才能访问Activity_A CustomAdapter_A

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 .您可以在Java_C_file_A类中为CustomAdapter_A创建 getter,并且您将在Activity_A拥有Java_C_file_A对象。 Here is sample code to access adapter这是访问适配器的示例代码

In Activity_A在活动_A

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM