How to call the Gridview Activity from another activity

余生长醉 提交于 2019-12-24 14:14:45

问题


I need an idea on how to call an activity that has a gridview from another activity. Basically, supposed my main activity has one button and when you click the button you are directed to another activity with the following sample code

 public void onClick(View v){
 if (v.getId() == R.id.button2) {
        Intent intent = new Intent(this, AnotherActivity.class);
        this.startActivity(intent);

    }
 }

But what if the activity that I'm being redirected to contains a gridview layout, how do I call that when I press the button? I don't have time to write my code here. It would be best if you just give me an idea or make a sample code thanks in advance.


回答1:


Your code should work on any Activity you have regardless of the layout they have. Just replace Intent intent = new Intent(this, AnotherActivity.class); with Intent intent = new Intent(this, ActivitywithGridView.class);

Remeber that you cannot see a GridView if it is not populated with data.




回答2:


You need to call ActivityName.this instead of this.

Instead of using this you could use ActivityName.this, it gets you the context of the Activity. Currently this is giving you onClick() method context reference.

Issue is Proper context is not passing so its not starting Activity.

You can try this code.

public void onClick(View v){
 if (v.getId() == R.id.button2) {
        Intent intent = new Intent(ActivityName.this, GridViewActivity.class);
        ActivityName.this.startActivity(intent);
    }
}


来源:https://stackoverflow.com/questions/33050043/how-to-call-the-gridview-activity-from-another-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!