0

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?

enter image description here

Some of the cell requires one text view only. But some others might required 2 to 3 more text views under the days.

12
  • 1
    simple_list_item_1 is a plain TextView (which is substituted with an AppCompatTextView when using appcompat). It is not a LinearLayout, and so cannot be cast as one. If you want the base item View to be a LinearLayout, then create your own layout that has a <LinearLayout> as the root, and replace android.R.layout.simple_list_item_1 with that R.layout. Commented Aug 15, 2018 at 1:26
  • I would also mention that this design might get a little hairy when dealing with item recycling upon scrolling, so if the possible number of 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 the Adapter. Commented Aug 15, 2018 at 1:30
  • @MikeM. I see I see. But then the number of text views is dynamic in this case. So I should create a list view to hold the text views, then place the list view into the cell of grid view? Commented Aug 15, 2018 at 1:34
  • No, I meant, for example, if the maximum possible number of 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 to GONE in getView(). Commented Aug 15, 2018 at 1:37
  • @MikeM. I see I see but do you have any examples I can follow for this case? Is there any way to keep this text view, then append another separate linear layout below it? Because some of the cells requires one text view only. For instance, the one at edited portion. Commented Aug 15, 2018 at 1:47

2 Answers 2

1

You can check simple_list_item_1.xml and see if the root of it is a TextView. If it is, it will throw a ClassCastException based on your example.

Sign up to request clarification or add additional context in comments.

Comments

0

Create a xml layout (ex. cell_layout.xml) and set it up the way you would want a cell to look like. Use a LinearLayout and place two TextViews inside. Give the two TextViews ids (ex. TextView1, TextView2). When you create the ArrayAdapter use: new ArrayAdapter<>(this, R.layout.cell_layout).

 //Add text for each cell to a string array String[] cell0 = new String[] {"Test", "Hi"}; //Add all cells to an another string array String[][] cells = new String[][] {cell0}; @Override public View getView(int position, View convertView, ViewGroup parent) { View cellView = convertView; if(cellView != null) { LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); cellView = layoutInflater.inflate(R.layout.scores, parent); } cellView.findViewById(R.id.textView1).setText(cells[position][0]); cellView.findViewById(R.id.textView2).setText(cells[position][1]); return cellView; } //This is a shortcut. Normally you would create a custom ArrayAdapter and a custom class 

7 Comments

I see I see but my intention is to place multiple text views in a cell. Is it doable with some modifications from the code I posted above? Or I have to use some custom adapter
Thanks! But let me double check with you regarding my understanding, basically cell0 contains the list of string I wanted to put in text view. Then what about the cells?
cell0 (also cell1, cell2, cell3 ...) contains a list of strings you would put in a single cell. Each TextView in a cell would be set to one of the strings in that list. cells is a list that would contain all the cells {cell0, cell1, cell2, cell3 ...}. If cell0 is a restaurant menu (restaurantMenu0) it would contain menu items {"pizza", "coca-cola"} and cells would contain a list of restaurants {restaurantMenu0, restaurantMenu1, restaurantMenu2}.
Hey I am getting error message java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference. I created a separate xml file and assign it to LayoutInflater
Change if(cellView != null) { to if(cellView == null) {
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.