单击按钮即可调用/修改当前活动
我有一个活动“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在适配器类中,我们可以使用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...如果我理解正确的话;您应该更新传递给适配器的列表,然后调用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.