I was having some problem when trying to add multiple text views into grid view cell. The number of text views is dynamic whereby it depends on the data in database. Here is how I set up the adapter for grid view:
adapter = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_list_item_1, items) { @Override public View getView(int position, View convertView, ViewGroup parent) { LinearLayout ll = (LinearLayout) super.getView(position, convertView, parent); // for simplicity I hardcoded two text views TextView cell = new TextView(this.getContext()); cell.setText("Test"); TextView cell2 = new TextView(this.getContext()); cell2.setText("HI"); ll.addView(cell); ll.addView(cell2); return ll; } }; gridView.setAdapter(adapter); My .xml file for grid view:
<GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="7" android:requiresFadingEdge="vertical" android:scrollbars="none" /> However, I am getting these error messages:
java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.LinearLayout at com.mainapp.scheduler.InstrumentSchedulerFragment$3.getView(InstrumentSchedulerFragment.java:586) at android.widget.AbsListView.obtainView(AbsListView.java:2365) at android.widget.GridView.onMeasure(GridView.java:1065) at android.view.View.measure(View.java:22071) at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1212) at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1552) at android.view.View.measure(View.java:22071) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602) at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) at android.view.View.measure(View.java:22071) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602) at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) at android.view.View.measure(View.java:22071) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514) at android.widget.LinearLayout.measureVertical(LinearLayout.java:806) at android.widget.LinearLayout.onMeasure(LinearLayout.java:685) at android.view.View.measure(View.java:22071) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602) at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) at com.android.internal.policy.DecorView.onMeasure(DecorView.java:724) at android.view.View.measure(View.java:22071) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2422) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1504) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1761) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911) at android.view.Choreographer.doCallbacks(Choreographer.java:723) at android.view.Choreographer.doFrame(Choreographer.java:658) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Any ideas why is it so?

Some of the cell requires one text view only. But some others might required 2 to 3 more text views under the days.
simple_list_item_1is a plainTextView(which is substituted with anAppCompatTextViewwhen using appcompat). It is not aLinearLayout, and so cannot be cast as one. If you want the base itemViewto be aLinearLayout, then create your own layout that has a<LinearLayout>as the root, and replaceandroid.R.layout.simple_list_item_1with thatR.layout.TextViews in an item is relatively small, it might be easier to add the maximum number of<TextView>s in the layout, and set their visibility accordingly in theAdapter.TextViews is 3, then go ahead and put 3<TextView>s in your<LinearLayout>, and if an item only needs 2, set the third one's visibility toGONEingetView().