单击按钮即可调用/修改当前活动

发布于 2024-12-05 21:48:27 字数 727 浏览 3 评论 0原文

我有一个活动“MyListActivity”。我有一个与之关联的适配器“MyGridAdapter”。它包含一个按钮网格(比如 3 个)。每个按钮都有一个“OnClickListner()”。 单击第一个按钮后,我必须采取相同的操作。因此,我决定重新使用同一页面,而不是创建类似的另一个活动。含义:我需要调用“MyListActivity”,但需要修改一些参数 - 增加按钮数量等... 我尝试在按钮单击时使用“invalidate()”。但它会重新加载相同的页面,而不会进行任何更改。

如何从适配器中的函数调用相同的活动?

我解决这个问题的方法是:我创建了另一个“tempActivity”。在其中一个函数中,我尝试启动上一个活动。 “MyGridAdapter”内的代码:

TempActivity temp=new TempActivity();
temp.setIntentObj(<parameters>);

“TempActivity”内:

Intent intent=new Intent(this,ListActivity.class);        //this gives a "NullPointerException"
startActivity(intent);

I have an activity "MyListActivity". I have an adapter associated with it "MyGridAdapter". It contains a grid of buttons (say 3). Each button has a "OnClickListner()".
On click of the 1st button, I would have to take the same action. So, instead of creating similar another activity, I decide to RE-USE the same page. Meaning: I need to call "MyListActivity" but with some modified parameters- increased number of buttons etc...
I tried to use, "invalidate()" on button click. But it re-loads the same page without any changes.

How to call the same activity from a function in the adapter?

My work around for the problem: I created another "tempActivity". in one of the function I am trying to start the previous activity.
code inside "MyGridAdapter":

TempActivity temp=new TempActivity();
temp.setIntentObj(<parameters>);

inside "TempActivity":

Intent intent=new Intent(this,ListActivity.class);        //this gives a "NullPointerException"
startActivity(intent);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朮生 2024-12-12 21:48:27

在适配器类中,我们可以使用context.startActivity(intent)启动一个活动我不知道我们可以从适配器访问“启动活动”...但现在它工作得很好!非常感谢您的推荐...

In the adapter class, we can start an activity using context.startActivity(intent) I did not know that we can access "start Activity" from adapter... but now it's working just fine!! Thanks a lot for your recommendation...

深海里的那抹蓝 2024-12-12 21:48:27

如果我理解正确的话;您应该更新传递给适配器的列表,然后调用adapter.notifyDataSetChanged()。 (方法名称可能有点不同,我在手机上打字,无法查找)。

您更新的列表必须与给定适配器的列表相同,因此您必须将其引用保留为类的属性。

If I understand you correctly; you should update the List that is passed to the Adapter and then invoke adapter.notifyDataSetChanged(). (method name might be a bit different, I'm typing on my mobile and can't look it up).

The List you update must be the same List that the adapter was given so you'll have to keep a reference of it as a property of your class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文