Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlice Emmely Sadza
4,205 Pointsam stuck.help
Now switch over to ProductsActivity.java and convert it to use the new GridView. Use the comments in the code as your guide to create and set mGridView in this simplified example.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
import android.os.Bundle;
import android.widget.GridView;
// This parent class no longer applies. Switch it to Activity
public class ProductsActivity extends ListActivity {
/*
* Some code has been omitted for brevity!
*/
// Declare mGridView here!
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products);
// Set mGridView here!
}
}
3 Answers
Steve Hunter
57,712 PointsHi there,
First, you need to amend the class inheritance - change that from ListActivity
to Activity
.
Where is says declare mGridView here
do just that. You just need a variable that can hold a GridView
. That looks like:
GridView mGridView;
Within onCreate()
you need to assign a value to mGridView
. You've already given it as id within the XML file, so you just need to link the two together with findViewById()
- don't forget to cast the returned type:
mGridView = (GridView) findViewById(R.id.gridView);
That should get you through.
Steve.
Alice Emmely Sadza
4,205 Pointsthanks you so much Steve...
MUZ140663 Question Ngwarati
11,782 Pointsimport android.os.Bundle; import android.widget.GridView;
// This parent class no longer applies. Switch it to Activity public class ProductsActivity extends Activity {
/*
* Some code has been omitted for brevity!
*/
// Declare mGridView here!
protected GridView mGridView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products);
// Set mGridView here!
mGridView = (GridView) findViewById(R.id.gridView);
}
}
Steve Hunter
57,712 PointsHi there,
That part of the challenge is amended where each of the commetns are placed.
It ends up looking like:
import android.os.Bundle;
import android.widget.GridView;
// This parent class no longer applies. Switch it to Activity
public class ProductsActivity extends Activity {
/*
* Some code has been omitted for brevity!
*/
// Declare mGridView here!
GridView mGridView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products);
// Set mGridView here!
mGridView = (GridView) findViewById(R.id.gridView);
}
}
Steve.