Following on from an earlier question I’m trying to get multiple (widgets or in this case WebViews) inside a GridView.
So I wasn’t really sure how to go about it whether to make my own adapter (seemed scary at the time) or create an ArrayAdapter<WebView>.
I wasn’t really sure if ArrayAdapter could accept it like this or if it only supported primitive types.
GridView contentGrid;
LinearLayout contentLayout;
WebWidget[] webWidgets;
WebWidget web1;
WebWidget web2;
WebWidget web3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web1 = new WebWidget(this, "http://www.google.ie");
web2 = new WebWidget(this, "http://www.facebook.com");
web3 = new WebWidget(this, "http://www.youtube.ie");
webWidgets = new WebWidget[]{web1,web2,web3};
contentGrid = (GridView) findViewById(R.id.contentGrid);
//Heres my attempt at an adapter for webview
ArrayAdapter<WebView> adapter = new ArrayAdapter<WebView>(this,
android.R.layout.simple_list_item_1, webWidgets);
contentGrid.setAdapter(adapter);
Unfortunately what comes out is the string version of the WebView.
So question is can I do it this way or am I better of making my own CustomAdapter?
NOTE: Could someone maybe point some information as to what the second constructor variable is? I.E android.R.layout.simple_list_item_1
No matter how you implement your
Adapter, you cannot reliably put scrollable widgets in other scrollable widgets, at least where they scroll in the same direction. Since bothWebViewandGridViewscroll vertically, you will get unreliable results trying to combine them this way.With that, on to some of your statements and questions:
An
ArrayAdaptercan adapt a Java array or anArrayListof whatever data type you like.The second constructor variable is what you want the cells in your
GridViewto look like. In your case, you are specifying a built-in layout resource that is aTextView. Hence, you are telling Android you want all your grid cells to beTextViewwidgets. Android will calltoString()on the objects in your array, pour that result into theTextView, and theTextViewswill go in your grid cells.If you want your
ArrayAdapterto be returning things other than aTextView, you will need to overridegetView()and handle more of that yourself, possibly using a layout file of your own creation.As noted at the outset of my answer, what you want simply will not work reliably.
Pretending for the moment that having
WebViewsin aGridViewwould work, the simplest solution would be for you to overridegetView()in your own subclass ofArrayAdapter, as I mentioned previously. Here is a free excerpt from one of my books that goes over this process.