[英]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.