How to call the Gridview Activity from another act

2019-07-31 23:06发布

问题:

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);
    }
}